这应该如何用 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
在每个 td 中找到第一个<font>
with class='spy1'
。国家在此之后的文本节点中。
rows = soup.find_all('td')
for row in rows:
country_label = row.find('font', _class='spy1')
if country_label:
print(country.next_sibling)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句