html - 用 beautifulsoup 解析 <br> 标签

标签 html web-scraping beautifulsoup tags web-crawler

我正在抓取一个网站,
标签的结构是:

<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/

相关文章:

python - 无法在 python 中使用 beautiful soup 解析 div 标签?

python - 查找另一个标签之前的标签 BeautifulSoup

javascript - 单击链接时维护主题标签值的干净方法是什么?

html - 响应式网格 - 垂直/水平堆叠,宽度/高度不断变化

python - 抓取特定标签和关键字,使用 BeautifulSoup 打印与之相关的信息

python - 如何通过Selenium从网站上抓取产品名称?

php - 将表单中的数据插入表不起作用,不返回错误

javascript - 显示不同的图像 react

python - 使用 Python 网页抓取下载 PDF 不起作用

python - 如何使用 Beautiful Soup 查找和更改标签之外的文本?