我正在尝试使用 scrapy 为学校项目下载一些内容。 我想获取每个页面的关键字列表,然后将其存储在数据库中。这就是我到目前为止所得到的。
scrapy shell http://news.nationalgeographic.com/2015/03/150318-pitcairn-marine-reserve-protected-area-ocean-conservation/
>>> response.xpath('//title/text()').extract()
[u'World\u2019s Largest Single Marine Reserve Created in Pacific']
>>> response.xpath("//meta[@name='keywords']")[0].extract()
u'<meta name="keywords" content="ocean life, conservationists, marine biodiversity, marine sanctuaries, wildlife conservation, marine protected areas, mpas, reserves, sanctuaries, ocean conservation">'
我想做的只是从元标记中提取内容,其中 name='keywords'
谢谢!
最佳答案
只需添加 /@content
即可提取 content
属性:
response.xpath("//meta[@name='keywords']/@content")[0].extract()
关于xpath - 使用scrapy从元标记中提取关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240581/