我想在每个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内容?


发布于 2016-05-31 11:45:26
您的第二个XPath查找a元素,该元素是div[@class="summary"]的直接子元素,并具有属性h3,该属性在HTML中不存在。
从XPath中获取h3中的a元素的正确方法如下:
h3/a/text()发布于 2016-05-31 16:08:47
另一种说法可能是:
questions = Selector(response).xpath('div[@class="summary"]/h3')为了从<a>获取数据
item['title'] = question.xpath('/a/text()').extract()[0]如果要提取的所有数据都在h3标记中,这是非常有用的。
https://stackoverflow.com/questions/37544295
复制相似问题