Python 我正在使用BeautifulSoup,我想获得img标签';s alt值

Python 我正在使用BeautifulSoup,我想获得img标签';s alt值,python,bs4,Python,Bs4,我想让一支棒球队的对手参加今天的比赛 所以我把这个编码了 此代码从Web站点获取今天的游戏信息 from bs4 import BeautifulSoup import datetime import urllib.request req = urllib.request.Request("http://www.hanwhaeagles.co.kr/html/game/1st_schedule_list1.asp") data = urllib.request.urlopen(req).rea

我想让一支棒球队的对手参加今天的比赛

所以我把这个编码了

此代码从Web站点获取今天的游戏信息

from bs4 import BeautifulSoup
import datetime
import urllib.request

req = urllib.request.Request("http://www.hanwhaeagles.co.kr/html/game/1st_schedule_list1.asp")
data = urllib.request.urlopen(req).read()

bs = BeautifulSoup(data, 'html.parser')

l = bs.find_all('div')
idx = 0

for s in l:
    try:
        prop = s.get('class')
        if prop != None and prop[0] == "box" and len(prop) == 2:
            l = s
            break
    except UnicodeEncodeError:
        print("Error")
    finally:
        idx += 1

print(l)
变量l是今天比赛的信息

img标签的alt值是对方球队的球队名称


我想把它打印出来。。。帮助我

,因为您对
类中存在的数据更感兴趣。您可以直接提取该类并进一步处理它:

from bs4 import BeautifulSoup
import datetime
import urllib.request

req = urllib.request.Request("http://www.hanwhaeagles.co.kr/html/game/1st_schedule_list1.asp")
data = urllib.request.urlopen(req).read()
bs = BeautifulSoup(data, 'html.parser')

for item in bs.select('.box'):
    team_name = item.find('img')['alt']
    print(team_name)

'NC'
'NC'
...
输出:

NC
NC
NC
KIA
KIA
KIA
두산
두산
삼성
삼성
넥센
넥센
SK
SK
NC
NC
롯데
롯데
KT
KT
KIA
KIA
SK
SK
LG
LG
KT

team_name=chunck[0]。查找('img')['alt']索引器:列表索引超出范围,我打印了chuck,它的值是[]我已根据您的代码编辑了答案,现在尝试感谢您。现在我知道了选择方法。今天的游戏是.box.Today,所以我将bs.select('.box')编辑为bs.select('.box.Today'),我解决了这个问题。我做到了。谢谢
NC
NC
NC
KIA
KIA
KIA
두산
두산
삼성
삼성
넥센
넥센
SK
SK
NC
NC
롯데
롯데
KT
KT
KIA
KIA
SK
SK
LG
LG
KT