gpt4 book ai didi

xpath - 在单个节点中获取所有包含html的文本scrapy xpath

转载 作者:行者123 更新时间:2023-12-03 17:24:51 27 4
gpt4 key购买 nike

response.xpath('//*[@id="blah"]//text()')


假设我的html是

<p id="blah">This is a simple text <a href="#">foo</a> and this is after tag. </p>


即使发生了什么,我也得到了一个文本列表,即使它是一个 <p>标记。如

[u'This is a simple text', u' and this is after tag.']


我实际的html内容很大,因此必须执行 join才能实现此目的。另外,我在 foo时丢失了 join。是否有任何特定的 xpath scrapy机制来执行此操作?

我想得到结果
这是一个简单的文本foo,位于标记之后。

请注意这里的 foo

谢谢

最佳答案

您可以将所有文本节点作为单个字符串获得,如下所示:

response.xpath('//*[@id="blah"]')[0].text_content()


输出:

'This is a simple text foo and this is after tag. '

关于xpath - 在单个节点中获取所有包含html的文本scrapy xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com