【发布时间】:2019-07-05 21:27:28
【问题描述】:
我希望在我的桌面目录中创建一个 csv 文件。
导入请求 从 bs4 导入 BeautifulSoup 将熊猫导入为 pd
url = "https://basketball.realgm.com/ncaa/conferences/Big-12-
Conference/3/Kansas/54/nba-players"
# get permission
response = requests.get(url)
# access html files
soup = BeautifulSoup(response.text, 'html.parser')
# creating data frame
columns = ['Player', 'Position', 'Height', 'Weight', 'Draft Year', 'NBA
Teams', 'Years', 'Games Played','Points Per Game', 'Rebounds Per Game',
'Assists Per Game']
df = pd.DataFrame(columns=columns)
table = soup.find(name='table', attrs={'class': 'tablesaw','data-
tablesaw-mode':'swipe','id': 'table-6615'}).tbody
trs = table.find('tr')
# rewording html
for tr in trs:
tds = tr.find_all('td')
row = [td.text.replace('\n', '')for td in tds]
df = df.append(pd.Series(row, index=columns), ignore_index=True)
df.to_csv('kansas_player', index=False)
我希望在我的桌面目录中创建一个 csv 文件。
【问题讨论】:
-
请帮助我刚刚开始进行网络解析。
-
如果
soup.find('table')找不到任何表怎么办? -
请发布完整的回溯,这有助于了解错误发生在哪里
标签: python pandas beautifulsoup html-parsing