Xpath scrapy filter过滤的选择器

时间:2018-08-30 09:04:39

标签: xpath scrapy

是否可以进行类似的操作(仅使用xpath):

filtered = response.xpath('//input[@type="submit"]')
filtered.xpath('[contains(@name, "abc")]')

上面的代码产生异常:

  

ValueError:XPath错误:[包含(@name,   “ abc”)]

1 个答案:

答案 0 :(得分:1)

您可以选择最初正确的元素:

filtered = response.xpath('//input[@type="submit"][contains(@name, "abc")]')

或过滤如下

filtered = response.xpath('//input[@type="submit"]')
# by exact @name value
new_filtered = [item for item in filtered if item.attrib['name'] == 'abc']
# by partial @name value
new_filtered = [item for item in filtered if 'abc' in item.attrib['name']]