gpt4 book ai didi

python - 关于 BeautifulSoup 中 get_text() 的建议

转载 作者:太空狗 更新时间:2023-10-29 18:13:50 25 4
gpt4 key购买 nike

我正在使用 BeautifulSoup 来解析 html 页面中的一些内容。

我可以从 html 中提取我想要的内容(即包含在由 span myclass 定义的 class 中的文本)。

result = mycontent.find(attrs={'class':'myclass'})

我得到这个结果:

<span class="myclass">Lorem ipsum<br/>dolor sit amet,<br/>consectetur...</span>

如果我尝试使用以下方法提取文本:

result.get_text()

我得到:

Lorem ipsumdolor sit amet,consectetur...

如您所见,标记 <br> 时被删除,内容之间不再有空格,两个单词被连接起来。

我该如何解决这个问题?

最佳答案

如果您使用的是 bs4,则可以使用 strings :

" ".join(result.strings)

关于python - 关于 BeautifulSoup 中 get_text() 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121001/

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