Reputation: 135
I want to get opposing team of one baseball team's today's game.
So I coded this.
This code get today's game's information from Web site.
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)
and 'variable l' is today's game's information.
img tag's alt value is opposing team's team name.
I want to print it... Help me
Upvotes: 0
Views: 1859
Reputation: 5950
Since you are more interested in data which exist inside box
class. You can directly extract that class and further process it :
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'
...
Upvotes: 1
Reputation: 3225
from bs4 import BeautifulSoup
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')
table = bs.find('table')
for tr in table.find_all('tr'):
for td in tr.find_all('td'):
if td.find('img'):
if 'alt' in td.find('img').attrs:
print(td.find('img')['alt'])
output:
NC
NC
NC
KIA
KIA
KIA
두산
두산
삼성
삼성
넥센
넥센
SK
SK
NC
NC
롯데
롯데
KT
KT
KIA
KIA
SK
SK
LG
LG
KT
Upvotes: 0