将find_all美丽的汤标签组合成一个字符串

law*_*son 1 html python beautifulsoup web-scraping

我正在使用beautifulsoup和html解析器进行抓取,并选择了要使用的html部分并将其保存为“容器”。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import ssl

my_url = 'https://www._________.co.uk/'
context = ssl._create_unverified_context()
uClient = uReq(my_url, context=context)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div",{"class":"row"})
Run Code Online (Sandbox Code Playgroud)

当涉及到在跨度中彼此相邻的几个标签时,我面临挑战。

我可以通过使用

company_string = container.span.find_all("b")
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

[<b>Company</b>, <b>Name</b>, <b>Limited</b>]
Run Code Online (Sandbox Code Playgroud)

如何抛弃标签并将它们组合成字符串,以便输出为“ Company Name Limited”?

原始html在这里:

<span class="company">
<a href="/cmp/Company-Name-Limited" onmousedown="this.href = 
appendParamsOnce(this.href, 'xxxx')" rel="noopener" target="_blank">
<b>Company</b> <b>Name</b> <b>Limited</b>
</a>
</span>
Run Code Online (Sandbox Code Playgroud)

aka*_*iya 5

使用 .text

>>> output = ' '.join([item.text for item in company_string])
'Company Name Limited'
Run Code Online (Sandbox Code Playgroud)