from bs4 import BeautifulSoup
from urllib.request import urlopen
webpage = urlopen("https://www.basketball-reference.com/leagues/NBA_2017_games-april.html")
soup = BeautifulSoup(webpage, 'html.parser')
data_rows = soup.find('table', {"id": "schedule"}).find_all('tr') # find all the 'tr' elements
for tr in data_rows:
if tr.text.strip() != "Playoffs": # check if were on the 'Playoffs' title tr
data = [td.text for td in tr.find_all(["td", "th"])]
print(data)
如果您使用的是beauthoulsoup的最新版本,请注意^{} is deprecated。请改用find_all()。类似地,使用.text而不是getText()
循环使用
tr
元素,确保您没有进入“季后赛”tr
,然后继续:如果您使用的是beauthoulsoup的最新版本,请注意^{} is deprecated 。请改用
find_all()
。类似地,使用.text
而不是getText()
相关问题 更多 >
编程相关推荐