如何使用XPath从SELECT列表中获取最后一个OPTION-Scrapy


问题内容

我正在使用此选择器,但出现错误

//*[@id="quantity"]/option/[last()-1]

如何选择上一个选项?

我正在使用Scrapy Framework。


问题答案:

/[使XPath表达式 无效 之前,还需要执行其他操作。去掉它:

//*[@id="quantity"]/option[last()-1]

请注意,您也可以使用Python / Scrapy解决它:

response.xpath('//*[@id="quantity"]/option')[-1].extract()

或者,以 CSS选择器 形式:

response.css('#quantity option:last-child').extract_first()
response.css('#quantity option')[-1].extract()