gpt4 book ai didi

python - 在 Scrapy 选择器中使用多个 xpath

转载 作者:太空宇宙 更新时间:2023-11-03 16:41:30 24 4
gpt4 key购买 nike

我必须像这样使用 Scrapy 抓取数据:

<div class="data"
data-name="{"id":"566565", "name":"data1"}"
data-property="{"length":"444", "height":"678"}"
>
data1
</div>
<div class="data"
data-name="{"id":"566566", "name":"data2"}"
data-property="{"length":"555", "height":"777"}"
>
data2
</div>

我需要data-namedata-property 属性。我的选择器是:

selections = Selector(response).xpath('//div[@class="data"]/attribute::data-property').extract()

如何在选择中包含 data-name 属性?

最佳答案

以下 XPath 应返回 data-propertydata-name 属性:

//div[@class='data']/attribute::*[name()='data-property' or name()='data-name']

XPath 演示:http://www.xpathtester.com/xpath/e720602b62461f3600989be73eb15aec

<小时/>

如果您需要为每个父 div 以某种格式成对返回两个属性,则无法使用纯 XPath 1.0 来完成此操作。需要一些Python,也许使用列表理解(未测试):

selections = [div.xpath('concat(@data-property, " ", @data-name)').extract() \
for div in Selector(response).xpath('//div[@class="data"]')]

关于python - 在 Scrapy 选择器中使用多个 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36739941/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com