使用Scrapy从元标记中提取关键字

9

我正在尝试使用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' 的 meta 标签中提取内容。
谢谢!
1个回答

27

只需添加/@content以提取content属性:

response.xpath("//meta[@name='keywords']/@content")[0].extract()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接