我正在尝试抓取网络数据,创建30个数据框。以下代码不起作用:
#import time & pandas
import time
import pandas as pd
franchises = {'atl':'ATL', 'bos':'BOS', 'brk':'BRK', 'chi':'CHI', 'cho':'CHO', 'cle':'CLE', 'dal':'DAL', 'den':'DEN', 'det':'DET', 'gsw':'GSW', 'hou':'HOU', 'ind':'IND', 'lac':'LAC', 'lal':'LAL', 'mem':'MEM', 'mia':'MIA', 'mil':'MIL', 'min':'MIN', 'nop':'NOP', 'nyk':'NYK', 'okc':'OKC', 'orl':'ORL', 'phi':'PHI', 'pho':'PHO', 'por':'POR', 'sac':'SAC', 'sas':'SAS', 'tor':'TOR', 'uta':'UTA', 'was':'WAS'}
#set up custom function to scrape contract dataframes from BB-Ref
def dfscrape(tm_nm):
url = 'https://www.basketball-reference.com/contracts/' + franchises[tm_nm] + '.html'
contracts = pd.read_html(url)[0]
time.sleep(1)
return contracts
dfscrape(tm_nm = 'atl')
分配URL的代码有效。但是,当我运行dfscrape(tm_nm ='atl')时,并不总是创建数据框“合同”。另外,我想在函数的每个实例上更改“合约”的名称,以便有30个数据框。
我应该使用for循环吗?我不知道如何反复为数据框分配新名称。