我正在抓取一个网站,
标签的结构是:
<div class="content"
<p>
"C Space"
<br>
"802 white avenue"
<br>
"xyz 123"
<br>
"Lima"
</p>
当我使用 beautifulsoup 使用以下命令获取文本时:
html=urlopen("something")
bsObj = BeautifulSoup(html,"html5lib")
templist = bsObj.find("div",{"class":"content"})
print(templist.get_text())
我得到以下输出: C Space802 白色 avenuexyz 123Lima
而我希望输出为:C Space 802 white avenue xyz 123 Lima。
如何在从后续 br 标签获取数据时添加额外的空格?
谢谢
最佳答案
你可以玩弄 .get_text()
参数:
In [4]: elm = soup.select_one(".content")
In [5]: print(elm.get_text(strip=True, separator=" "))
"C Space" "802 white avenue" "xyz 123" "Lima"
关于html - 用 beautifulsoup 解析 <br> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43644933/