【问题标题】:Xpath scrapy filter the filtered selectorXpath scrapy过滤过滤后的选择器
【发布时间】:2018-08-30 17:18:33
【问题描述】:

是否可以制作类似的东西(仅使用 xpath):

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

以上代码产生异常:

ValueError: XPath 错误: [contains(@name, "abc")]

【问题讨论】:

    标签: xpath scrapy


    【解决方案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']]
    

    【讨论】:

    • 感谢您对使用 attrib 属性的建议,但我想知道是否可以仅在第二个过滤器中使用 xpath 选择器。
    • AFAIK XPath 中没有这样的功能。您可以使用filtered.xpath('./parent::*/input[contains(@name, "abc")]'),但这只是一种解决方法
    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多