0

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")
1
  • url in your code is a string. What is url.json() supposed to do? Commented Mar 18, 2018 at 19:34

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Oh wow thanks, I added in the request but now I'm getting this
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
can you give me a url, with row data fromatted into the string
looks like, sometimes nothing is being returned. check the following link , stackoverflow.com/questions/8381193/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.