猿问

通过beautifulsoup获得以下HTML标记内的数字?

我可以通过beautifulsoup在以下HTML标签中获取数字吗?


<tr align="center" height="15" id="tr_1599656" bgcolor="#ffffff" index="0"></tr>

<tr align="center" height="15" id="tr_1599657" bgcolor="#ffffff" index="1"></tr>

<tr align="center" height="15" id="tr_1599644" bgcolor="#ffffff" index="2"></tr>

我尝试过的Python代码


from bs4 import BeautifulSoup

import re


html_code = """"

<tr align="center" height="15" id="tr_1599656" bgcolor="#ffffff" index="0"></tr>

<tr align="center" height="15" id="tr_1599657" bgcolor="#ffffff" index="1"></tr>

<tr align="center" height="15" id="tr_1599644" bgcolor="#ffffff" index="2"></tr>

"""

soup = BeautifulSoup(html_code,'html.parser')

rows = soup.findAll("tr", {"id" : re.compile('tr_*\d')})

print rows

预期产量


1599656

1599657

1599644


守候你守候我
浏览 220回答 2
2回答

HUH函数

soup=BeautifulSoup('<tr align="center" height="15" id="tr_1599656" bgcolor="#ffffff" index="0"></tr><tr align="center" height="15" id="tr_1599657" bgcolor="#ffffff" index="1"></tr><tr align="center" height="15" id="tr_1599644" bgcolor="#ffffff" index="2"></tr>')lines=soup.find_all('tr')for line in lines:print(re.findall('\d+',line['id'])[0])请下次自行尝试一次。

噜噜哒

假设所有id属性都遵循模式tr_XXXXXXX。此代码将适用于它from bs4 import BeautifulSoupsoup = BeautifulSoup(html_code,'html.parser')for t in soup.findAll('tr'):&nbsp; &nbsp; print(t['id'][3:])输出159965615996571599644变量html_code包含您在问题中发布的一段html代码
随时随地看视频慕课网APP

相关分类

Python
我要回答