Nick
Nick

Reputation: 394

Python loop through URL's

I'm attempting take dates from a dataframe and loop through them within URL's. I've managed to print the URL's (1st code), but when I attempt to turn the URL's json into a dataframe (2nd code) I get this response.

AttributeError: 'str' object has no attribute '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)

Any ideas on what I'm doing wrong?

#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")

Upvotes: 0

Views: 216

Answers (1)

rawwar
rawwar

Reputation: 4992

I think you forgot to request the URL. you should send a request and if the response is a json, you should parse it

Upvotes: 1

Related Questions