Html 使用BeautifulSoup的Python网页抓取,如何合并两个<;p>;将文本转换为列表的一个元素

Html 使用BeautifulSoup的Python网页抓取,如何合并两个<;p>;将文本转换为列表的一个元素,html,python-3.x,beautifulsoup,Html,Python 3.x,Beautifulsoup,我用BeautifulSoup做网页抓取,将结果放入列表, html显示如下: 2013大众帕萨特 情况:非常好 使用列表的功能 title = [] for h in header: title.append(h.get_text()) title = ''.join([title]) 否则,向列表中添加元素而不是文本,并使用列表理解连接文本 title = [] for h in header: title.append(h) title = '

我用BeautifulSoup做网页抓取,将结果放入列表, html显示如下:

2013大众帕萨特

情况:非常好

使用列表的功能

title = []
for h in header:
        title.append(h.get_text())
title = ''.join([title])
否则,向列表中添加元素而不是文本,并使用
列表理解
连接文本

title = []
for h in header:
        title.append(h)
title = ''.join([i.text for i in title])

希望这有帮助!干杯

您可以使用stripped_字符串

from bs4 import BeautifulSoup


html = """<p class="attrgroup">
        <span><b>2013 Volkswagen Passat</b></span>
        <br>
      </p>
      <p class="attrgroup">
        <span>condition: <b>excellent</b></span>
        <br>
</p>"""

tag = BeautifulSoup(html, 'html.parser')

data = (' '.join(tag.stripped_strings))
print data
从bs4导入美化组
html=“”

2013大众帕萨特

情况:非常好

“”“ tag=BeautifulSoup(html,'html.parser') 数据=(''.join(tag.stripped_字符串)) 打印数据