我正在嘗試從 nba.com 匯總 NBA 球員資料。我有一個 csv 中的玩家 ID 串列。我想加載每個玩家 ID 并將其傳遞到引數串列中。然后在requests.get 中使用引數串列。當我直接輸入玩家 ID 作為引數時,request.get 起作用。對 csv 回圈通過該函式的測驗似乎有效。但是,我無法成功將 playerids 傳遞到引數串列中。我試過查看類似的 nba python 代碼,但看不出我哪里出錯了。
'''
import pandas as pd
import requests
import csv
best_db=pd.DataFrame()
def table_Scrape():
global best_db
with open("SHORT_ID_plyr.csv", "r") as f_urls:
f_urls_list = csv.reader(f_urls, delimiter=',')
next(f_urls_list)
##step5. open 1st url from .csv
for lines in f_urls_list:
u = lines[0]
print(u) #<loop test
# import requests
player_id = u
url = """
http://stats.nba.com/stats/playergamelogs?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerID=203932&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular Season&ShotClockRange=&VsConference=&VsDivision=
"""
#url = """
#https://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular Season&ShotClockRange=&StarterBench=&TeamID=0&TwoWay=0&VsConference=&VsDivision=&Weight=
#"""
header_dict = {
'User-Agent': 'Mozilla/5.0',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Referer': 'https://stats.nba.com',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Host': 'stats.nba.com'
}
params = {
'LastNGames': '0',
'LeagueID': '00',
'MeasureType': 'Base',
'Month': '0',
'OpponentTeamID': '0',
'PORound': '0',
'PaceAdjust': 'N',
'PerMode': 'Totals',
'Period': '0',
'PlayerID': u,
'PlusMinus': 'N',
'Rank': 'N',
'Season': '2021-22',
'SeasonType': 'Regular Season'
}
res = requests.get(url, headers=header_dict, params=params)
json_set = res.json()
headers = json_set['resultSets'][0]['headers']
data_set = json_set['resultSets'][0]['rowSet']
df = pd.DataFrame(columns=headers)
df.head #test the dataframe NOTE: does not appear to be working either
table_Scrape() #call the function
uj5u.com熱心網友回復:
您正在將有效負載傳遞到已經具有硬編碼播放器 ID 的有效負載的 url。將 palyer_id 變數放入 url
import pandas as pd
import requests
best_db=pd.DataFrame()
def table_Scrape():
df = pd.read_csv('SHORT_ID_plyr.csv')
player_id_list = list(df['PLAYER_ID'])
for player_id in player_id_list:
print(player_id)
url = f"""http://stats.nba.com/stats/playergamelogs?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerID={player_id}&PlusMinus=N&Rank=N&Season=2021-22&SeasonSegment=&SeasonType=Regular Season&ShotClockRange=&VsConference=&VsDivision="""
header_dict = {
'User-Agent': 'Mozilla/5.0',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Referer': 'https://stats.nba.com',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Host': 'stats.nba.com'
}
res = requests.get(url, headers=header_dict)
print(res.text)
json_set = res.json()
headers = json_set['resultSets'][0]['headers']
data_set = json_set['resultSets'][0]['rowSet']
df = pd.DataFrame(data_set,columns=headers)
print(df.head()) #test the dataframe NOTE: does not appear to be working either
table_Scrape() #call the function
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/368366.html
上一篇:如何在awk腳本中隱藏csv內容
下一篇:用美麗的湯只找到表中的某些元素
