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

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

無法獲取 HTML 標簽內的數據

無法獲取 HTML 標簽內的數據

森林海 2021-06-06 07:50:38
無法獲取 HTML 標簽“alt”= 內的數據from bs4 import BeautifulSoupimport resoup=BeautifulSoup("""<div class="couponTable">    <div id="tgCou1" class="tgCoupon couponRow"><span class="spBtnMinus"></span><!-- react-text: 67 -->Wednesday Matches<!-- /react-text --></div>    <div class="cflag"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"></div>    <div class="cflag"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"></div>    </div></div></div>""")lines=soup.find_all('div')line in lines:print(re.findall('\w+',line['alt'])[0])
查看完整描述

1 回答

?
catspeake

TA貢獻1111條經驗 獲得超0個贊

如果您只需要該alt值,那么您最好獲取img標簽而不是div標簽。也不需要使用正則表達式來提取alt值


from bs4 import BeautifulSoup

import re

soup=BeautifulSoup("""<div class="couponTable">

<div id="tgCou1" class="tgCoupon couponRow"><span class="spBtnMinus"></span><!-- react-text: 67 -->Wednesday Matches<!-- /react-text --></div>

<div class="cflag"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"></div>

<div class="cflag"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"></div>

</div></div></div>""",'html.parser')


lines=soup.find_all('img')

for line in lines:

    print(line['alt'])

輸出


日本聯賽杯

日本聯賽杯


查看完整回答
反對 回復 2021-06-09
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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