BeautifulSoup 中 get_text() 的建议 [英] Suggestions on get_text() in BeautifulSoup
本文介绍了BeautifulSoup 中 get_text() 的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 BeautifulSoup 来解析 html 页面中的一些内容.
I am using BeautifulSoup to parse some content from a html page.
我可以从 html 中提取我想要的内容(即包含在 class
myclass 定义的 span
中的文本).
I can extract from the html the content I want (i.e. the text contained in a span
defined by the class
myclass).
result = mycontent.find(attrs={'class':'myclass'})
我得到了这个结果:
<span class="myclass">Lorem ipsum<br/>dolor sit amet,<br/>consectetur...</span>
如果我尝试使用以下方法提取文本:
If I try to extract the text using:
result.get_text()
我得到:
Lorem ipsumdolor sit amet,consectetur...
正如你看到的,当标签
被移除时,内容之间没有更多的间距,两个词被连接起来.
As you can see when the tag <br>
is removed there is no more spacing between the contents and two words are concated.
我该如何解决这个问题?
How can I solve this issue?
推荐答案
如果你使用的是 bs4 你可以使用 strings
:
If you are using bs4 you can use strings
:
" ".join(result.strings)
这篇关于BeautifulSoup 中 get_text() 的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文