首页 > 解决方案 > 使用 xpath 选择 html 属性的不同值

问题描述

我需要从这个代码中选择reference值和其他一些值(例如) (或者如果它更好)。delivery_out_of_stockxpathcss

关于如何处理它的任何建议?

<div class="tab-pane" id="product-details" data-product="{"id_shop_default":"1","id_manufacturer":"993","id_supplier":"0","reference":"0165926","is_virtual":"0","delivery_in_stock":"","delivery_out_stock":"","id_category_default":"1006","on_sale":"0","online_only":"0","ecotax":0,"minimal_quantity":"1","low_stock_threshold":null}" role="tabpanel">
</div>

标签: pythonscrapy

解决方案


我会使用 xpath 来获取数据产品中的数据,然后将 json 作为字典加载以查找所需的值:

import json
# xpath to get the data in data-product
data_product = response.xpath('//*[@id="product-details"]/@data-product').extract_first()
# load the json in a dictionary
data = json.loads(data_product)
# get the value by key
reference = data['refererce']
delivery_out_of_stock = data['delivery_out_of_stock']

推荐阅读