Python循环通过URL

时间:2018-03-18 19:32:32

标签: python

我正在尝试从数据框中获取日期并在URL中循环它们。我已经设法打印了URL(第一个代码),但是当我尝试将URL的json转换为数据帧(第二个代码)时,我得到了这个响应。

  
    
      

AttributeError:'str'对象没有属性'json'

    
  
#1st code
import requests
import pandas as pd

df = pd.read_csv('NBADates.csv')
df.to_dict('series')

for row in df.loc[ : ,"Date"]:
    url = url_template.format(row=row)
    print(url)

关于我做错的任何想法?

#2nd code
import requests
import csv
import pandas as pd

url_template = "https://stats.nba.com/stats/leaguedashptstats?College=&Conference=&Country=&DateFrom={row}&DateTo={row}&Division=&DraftPick=&DraftYear=&GameScope=&Height=&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PerMode=Totals&PlayerExperience=&PlayerOrTeam=Player&PlayerPosition=&PtMeasureType=SpeedDistance&Season=2017-18&SeasonSegment=&SeasonType=Regular+Season&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="

df = pd.read_csv('NBADates.csv')
df.to_dict('series')

for row in df.loc[ : ,"Date"]:
    url = url_template.format(row=row)

    stats = url.json()['resultSets'][0]['rowSet']
    headers = url.json()['resultSets'][0]['headers']
    stats_df = pd.DataFrame(stats, columns=headers)

    # Append to the big dataframe
    lineup_df = lineup_df.append(stats_df, ignore_index=True)

lineup_df.to_csv("Stats.csv")

1 个答案:

答案 0 :(得分:1)

我想您忘了请求该网址。你应该发送一个请求,如果响应是一个json,你应该解析它