在Python中使用BeautifulSoup模块中的get_text()函数时,它返回与HTML源代码匹配的带换行符的文本
但是,我希望换行符模仿您在浏览器中看到的内容(例如,忽略HTML源代码中的换行符,一个<br>
标记的换行符,两个<p>
标记之间的换行符)
from bs4 import BeautifulSoup
some_html = """<p>Some
sample html<br>
new line
<p>New paragraph"""
plain_text = BeautifulSoup(some_html,"html.parser").get_text()
预期结果:
Some sample html
new line
New paragraph
实际结果:
Some
sample html
new line
New paragraph
最后我用了一些替代品。它适用于我正在使用的HTML
相关问题 更多 >
编程相关推荐