亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過beautifulsoup獲得以下HTML標記內的數字?

通過beautifulsoup獲得以下HTML標記內的數字?

守候你守候我 2021-05-06 07:11:59
我可以通過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])

請下次自行嘗試一次。


查看完整回答
反對 回復 2021-05-25
?
嚕嚕噠

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代碼




查看完整回答
反對 回復 2021-05-25
  • 2 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號