python - 无法从以下 html 代码中打印出 [0] 数据
问题描述
我可以从 html 中打印出 [0] 吗?
from bs4 import BeautifulSoup
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="couponRow rAlt1 tgCou1" id="rmid20180905WED1"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"><img src="/ContentServer/jcbw/images/icon_tv-C661.gif?CV=L302R1g" alt="C661-i-CABLE 661 C601-i-CABLE 601" title="C661-i-CABLE 661 C601-i-CABLE 601"></span></span><img src="/football/info/images/btn_odds.gif?CV=L302R1g" alt="All Odds" title="All Odds"></a></div><div class="couponRow rAlt0 tgCou1" id="rmid20180905WED2"><img src="/ContentServer/jcbw/images/flag_JLC.gif?CV=L302R1g" alt="Japanese League Cup" title="Japanese League Cup" class="cfJLC"><img src="/ContentServer/jcbw/images/icon_tv-C662.gif?CV=L302R1g" alt="C662-i-CABLE 662 C602-i-CABLE 602" title="C662-i-CABLE 662 C602-i-CABLE 602"></span></span><img src="/football/info/images/btn_odds.gif?CV=L302R1g" alt="All Odds" title="All Odds"></a></div></div></div>""",'html.parser')
lines=soup.find_all('img')
for line in lines:
print(line['alt'])
输出
Japanese League Cup
C661-i-CABLE 661 C601-i-CABLE 601
All Odds
Japanese League Cup
C662-i-CABLE 662 C602-i-CABLE 602
All Odds
预期产出
Japanese League Cup
Japanese League Cup
解决方案
查看您对其他人的回复,您似乎不想要所有图像的替代文本,而只想要指定的图像。您给出的(诚然很小的)示例的共同点是它们共享一个类。这可能是你要找的吗?
lines=soup.find_all('img', class_='cfJLC')
for line in lines:
print(line['alt'])
输出:
Japanese League Cup
Japanese League Cup
推荐阅读
- node.js - Webpack + Typescript,无法为 npm 包创建自己的 .d.ts 文件
- javascript - 我可以使用 mailgun 中的沙箱域发送电子邮件,但不能使用经过验证的真实域
- reactjs - 使用 gatsby 博客数据在循环内渲染一个反应组件
- node-gyp - 在 macOS High Sierra 10.13.6 上安装 node-gyp 时出错
- jquery - 如何在 django 视图中获取 html 输入的属性值
- c - 在 main 中调用一个函数,该函数具有结构数组和函数参数中的浮点变量数组
- html - 如何使用论坛反应动态更改按钮样式?
- c# - 在我使用 xamarin prism 在调试模式下重新保存 xaml 页面之前,CollectionView 不会显示元素
- sql - 应用程序的 SQL 触发器
- python - 重定向到文件的 Powershell 输出上的拉丁重音问题