【发布时间】:2016-05-31 11:20:08
【问题描述】:
我想提取每个div class="summary" 中的网页内容。
并且在每个summary div中,我想提取div中每个类的数据。
以下是我的sn-p。
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 内容?
【问题讨论】:
-
如果你删除你的 [0],你会得到什么输出?
-
@Error404E,空列表
[]。所以xpath参数有问题 -
我从来没有用过 Scrapy,但是如果你把你的第一个 xpath 像这样:
//div[@class="summary"]/h3? -
@Error404,我试过了,我工作正常,但我需要更多数据。
-
然后,正如@har07 的回答所建议的,你必须在
a之前阅读你的h3,因为a是h3的孩子。
标签: python css xpath scrapy web-crawler