【发布时间】:2021-06-28 14:47:08
【问题描述】:
这应该如何使用 bs4 解析以仅获取国家/地区值?
<tr>
<td colspan=16>
<font class=spy6><u>
<font class=spy1>Country</font> Brazil/BR<br>
<font class=spy1>Region </font> South America/SA <font class=spy1>TimeZone=America/Sao_Paulo
<font class=spy1>ORG/ASN</font> Brazil/BRORG/ASN<br></u></font>
</td>
</tr>
通过这段代码,我得到了 td 中的所有文本,但我不明白如何专门引用 Country
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, 'html.parser')
rows = soup.find_all('td')
for row in rows:
print(row.text)
我得到了这个字符串
Country Brazil/BR South America/SA TimeZone=America/Sao_Paulo Location=-22.8305/-43.2192REG
Brazil/BRORG/ASN Locaweb Servicos de Internet S/A/27715
【问题讨论】:
-
row.find_all("font", {"class": "spy1"})?或find而不是find_all仅获取第一个元素row.find("font", {"class": "spy1"}) -
国名不在
<font>内,只包含标签Country
标签: python parsing web-scraping beautifulsoup