gpt4 book ai didi

python - Scrapy,如何提取h3内容?

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

我想提取每个 div class="summary" 中的网页内容。在每个 summary div 中,我想提取 div 中每个类中的数据。

以下是我的片段。

questions = Selector(response).xpath('//div[@class="summary"]')
for question in questions:
item = StackItem()
# get the hyperlink of h3 text
item['title'] = question.xpath('a[@h3]/text()').extract()[0]
yield item

我应该如何在我的代码中编写 xpath 内容?

enter image description here enter image description here

最佳答案

您的第二个 XPath 查找 a 元素,该元素是 div[@class="summary"] 的直接子元素并且具有属性 h3,它在发布的 HTML 中不存在。

div 获取 h3 中的 a 元素的正确 XPath 如下:

h3/a/text()

关于python - Scrapy,如何提取h3内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37544295/

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