python - 替换 beautifulsoup 中的 <p> 和 <br> 标签

标签 python beautifulsoup

您好,我目前正在寻找一种用空格替换代码中标签的方法。

soup = BeautifulSoup("<p>Something</p><p>Something</p>") 
print soup.get_text()

SomethingSomething

#When I do get_text now I would get SomethingSomething but I want Something Something

最佳答案

get_text 函数允许您指定分隔各个元素的文本的内容:

In [1]: from bs4 import BeautifulSoup

In [2]: soup = BeautifulSoup("<p>Something</p><p>Something</p>")

In [3]: print soup.get_text(separator=u' ')
Something Something

引用:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text

关于python - 替换 beautifulsoup 中的 <p> 和 <br> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34137370/

相关文章:

python - 网页抓取 - Python - 在 html 中找不到链接

python - 如何使用 GeoDataFrame 生成 Folium map ?

python - 读取 yahoofinancials 的输出

python - 如何从 beautifulsoup (Python) 中的表中删除列

python - BeautifulSoup 4 : Remove comment tag and its content

python - 网页抓取工具不会转到正确的页面

python - beautifulsoup - 查找 div 中的所有 li

python - numpy 3d 数组 -- 展平 --> 1d 数组 --> 选择 1d 中的一个元素 --> 如何知道该元素在 3d 中的索引?

python - scipy.stat.norm.pdf 加起来不等于一

Python - 对于文本文件中的每个值?