使用scrapy从metatag中提取关键字

Nan*_*ncy 4 xpath meta-tags scrapy web-scraping

我正在尝试使用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">'
Run Code Online (Sandbox Code Playgroud)

我想做的只是从meta标签中提取内容,其中name ='keywords'

谢谢!

har*_*r07 14

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

response.xpath("//meta[@name='keywords']/@content")[0].extract()
Run Code Online (Sandbox Code Playgroud)