我正在抓取这个url:http://quotes.toscrape.com/
当我这样做的时候,它工作得很好:
response.xpath("//meta[@itemprop='keywords']/@content").extract()
response.xpath("//meta[@itemprop='keywords'][1]/@content").extract_first()但是当我尝试使用索引从该元数据列表中获取第二个元数据时
response.xpath("//meta[@itemprop='keywords'][2]/@content").extract_first()它不起作用。
我遗漏了什么?
谢谢!
发布于 2018-05-01 02:00:26
您需要将表达式放在括号中的索引之前:
而不是:
"//meta[@itemprop='keywords'][2]/@content"它应该是:
"(//meta[@itemprop='keywords'])[2]/@content"这是必需的,因为您的xpath中有参数运算符。
您可以测试以下内容:
$ scrapy shell "http://quotes.toscrape.com/"
In [1]: response.xpath("//meta[@itemprop='keywords'][2]/@content").extract_first()
In [2]: response.xpath("(//meta[@itemprop='keywords'])[2]/@content").extract_first()
Out[2]: 'abilities,choices'https://stackoverflow.com/questions/50105661
复制相似问题