簡體   English   中英

如何使用 xpath (Scrapy) 從 dict 獲取數據

[英]How do I get data from dict using xpath (Scrapy)

我正在嘗試使用 xpath 從這家餐廳頁面獲取供應商 ID,但我不知道如何獲取,因為它在字典中。 這就是我嘗試過的//*[@class="vendor"]然后感到困惑

<section class ="vendor" data-vendor="{"id":20707,"name":"Beard Papa\u0027s (Novena)","code":"x1jg","category":"Dessert,Bakery and Cakes","is_active":true,"timezone":"Asia\/Singapore","available_in":"2020-11-11T10:00:00+0800","city_id":1,"latitude":1.320027,"longitude":103.843897,"is_pickup_available":true,"is_delivery_available":true,"url_key":"beard-papas-novena","ddt":30,"chain":{"id":1992,"name":"Beard Papa","url_key":"beard-papa","is_accepting_global_vouchers":true,"code":"ck0vb","main_vendor_code":"v3lv"},"rating":4.6,"review_number":224,"minOrder":0,"deliveryFee":0,"is_promoted":false,"is_premium":false,"toppings":[],"accepts_instructions":true,"category_quantity":3,"loyalty_program_enabled":false,"loyalty_percentage_amount":0,"has_delivery_provider":true,"vertical":"restaurants","is_preorder_enabled":true,"is_vat_visible":true}">

正確的方法(正如booleantrue已經指出的那樣,import json和下一步:

data_vendor = response.xpath('//section[@class="vendor"]/@data-vendor').get()
data_vendor = json.loads(data_vendor)
vendor_id = data_vendor['id']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM