我可以通过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 BeautifulSoupimport rehtml_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预期产量159965615996571599644
2 回答

HUH函数
TA贡献1836条经验 获得超4个赞
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])
请下次自行尝试一次。

噜噜哒
TA贡献1784条经验 获得超7个赞
假设所有id属性都遵循模式tr_XXXXXXX。此代码将适用于它
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_code,'html.parser')
for t in soup.findAll('tr'):
print(t['id'][3:])
输出
1599656
1599657
1599644
变量html_code包含您在问题中发布的一段html代码
添加回答
举报
0/150
提交
取消