【问题标题】:Load csv and pass each value into a url parameter?加载 csv 并将每个值传递给 url 参数?
【发布时间】:2022-01-05 05:35:15
【问题描述】:

我正在尝试从 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

【问题讨论】:

  • 提供SHORT_ID_plyr.csv文件的样本
  • PLAYER_ID 1629760 1629752 1629750 1629744 1629741 1629740 1629738 1629735
  • 你能把 csv 文件贴在某个地方,让我看看文件是如何被读入的吗?
  • 当然,我的错 - 这是怎么回事:drive.google.com/file/d/1DscQ-JM03jRP7i7ZNZP2I6QzyYzS7l0N/…
  • 好的,刚刚向您发送了访问请求

标签: python pandas csv web-scraping parameter-passing


【解决方案1】:

您正在将有效负载传递到已经具有带有硬编码播放器 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

【讨论】:

  • 谢谢!这是一个非常有用的解释。但是,我尝试运行代码并且遇到了一个新错误。在 json_set = res.json() 的第 42 行,我得到一个 jsondecodeerror。我尝试运行您的完整代码块并直接对我的代码进行更改,但仍然遇到问题。
  • 在什么 ID 上被绊倒了?
  • 换句话说,错误前屏幕上的最后一个print(u)是什么?
  • 第一个 player_id 打印后我卡住了
  • 好的。让我下载你的文件并在我这边调试
猜你喜欢
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 2020-03-21
  • 1970-01-01
  • 2013-09-25
  • 2012-10-08
相关资源
最近更新 更多