【发布时间】:2019-02-09 04:10:21
【问题描述】:
无法获取 HTML 标签 "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>""")
lines=soup.find_all('div')
line in lines:print(re.findall('\w+',line['alt'])[0])
【问题讨论】:
-
您已经在使用 beautifulsoup 来获取 div。那你为什么要使用正则表达式?
-
for div in lines: imgs = div.find_all("img") for img in imgs: print(img['alt']) -
谢谢,亲爱的,解决了@DanielRoseman
-
谢谢,亲爱的,解决了@WiktorStribiżew
标签: python regex beautifulsoup