gpt4 book ai didi

python - 如何在Scrapy中使用Xpath表达4位数字

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:10 25 4
gpt4 key购买 nike

目的是使用 Scrapy 抓取网站。我想要得到的东西是每个 div 之间都有唯一的 4 位数字 id,如下所示:

<div id="3456" ...> Item 1 </div>
<div id="5643" ...> Item 2 </div>
<div id="8767" ...> Item 3 </div>

我需要知道如何在以下命令中通用地定义 4 位数字,以便我可以转到每个项目进行抓取。

for sel in response.xpath('//div[@id="4-digit-number-description"]'):

最佳答案

使用Scrapy,您可以使用regular expressions inside the XPath expressions ,非常方便:

response.xpath('//div[re:test(@id, "\d{4}")]')

关于python - 如何在Scrapy中使用Xpath表达4位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928077/

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