【问题标题】:Xpath for text between tag with space in between after every tag change每次标签更改后标签之间带有空格的文本的 Xpath
【发布时间】:2022-01-21 10:01:43
【问题描述】:

文字就像

<tag>Head Office41-43 Ricketts Road<tag/> <tag>Mount Waverley, Melbourne<tag/>

我得到的结果是

Head Office41-43 Ricketts RoadMount Waverley, Melbourne

但我想要它

Head Office41-43 Ricketts Road Mount Waverley, Melbourne

我使用的代码是

response.xpath('string(normalize-space(//*/text()[contains(normalize-space(), "{}")]/../..))'.format(marker))

其中标记是文本。

所以我想要Road Mount而不是RoadMount

查看包装的标签: enter image description here

这是我正在使用的代码 enter image description here

【问题讨论】:

    标签: xpath xpath-2.0 normalize-space


    【解决方案1】:

    您的 xpath 正在返回多个值 - 您所要做的就是用分隔符将它们连接起来:

    xpath = 'string(normalize-space(//*/text()[contains(normalize-space(), "{}")]/../..))'.format(marker))
    results = " ".join(response.xpath(xpath).getall())
    #                                        ^^^^^^^^
    # note: don't forget to use getall to retrieve all results
    print(results)
    

    【讨论】:

    • 抱歉,它不起作用。
    • @TusharDeora 你用的是什么xpath客户端?
    • 我正在使用scrapy 2.5.0。我不知道如何查看 xpath 客户端。我在网上查过,但没有得到任何信息。
    • @Grantitosauraus 请看我的新编辑。
    • @TusharDeora 啊,那么您应该使用getall() 来检索您的xpath 查询的所有结果。查看我的编辑。
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2018-02-25
    相关资源
    最近更新 更多