为了账号安全,请及时绑定邮箱和手机立即绑定

无法获取 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 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号