简体   繁体   中英

Python loop through Url Range

The script below should go to 3 different pages and write all three of those to a csv. However it is only writing the last page. If I print(my_data) it also only print the last page. What that is telling me is it is not the write to csv which is the error but within the for i in range(41244,41246) .

Here is my code:

import base64
import requests
import json
import csv

USERNAME, PASSWORD = 'admin', 'admin'

for i in range(41244,41246):
    req = requests.get(
        url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/game_startinglineup.json?gameid=" + str(i)
        , 
        headers={
            "Authorization": "Basic " +
                    base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
                                    .encode('utf-8')).decode('ascii')
        }
     )
req.raise_for_status()
data = req.json()
my_data =[]

for i in range(2):
    EX_team_home = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']

for i in range(1):
    EX_team_away = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']

for i in range(20):
    EX_homepos = EX_team_home[i]['position']
    EX_awaypos = EX_team_away[i]['position']
    EX_homename = EX_team_home[i]['player']['LastName']
    EX_awayname = EX_team_away[i]['player']['LastName']

    my_data.append([EX_homepos, EX_homename, EX_awaypos, EX_awayname])

print(my_data)

header = ["EX_homepos", "EX_homename", "EX_awaypos", "EX_awayname"]

with open("TESTLINE3.csv", 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    writer.writerows(my_data)
    f.close()

EX_homepos EX_homename EX_awaypos EX_awayname Goalie-Backup Hutton Goalie-Backup Budaj ForwardLine1-RW Sobotka ForwardLine1-RW Kucherov ForwardLine2-C Schenn ForwardLine2-C Johnson ForwardLine4-C Brodziak ForwardLine4-C Bournival ForwardLine3-LW Paajarvi ForwardLine3-LW Palat ForwardLine4-RW Thorburn ForwardLine4-RW Paquette ForwardLine3-C Sundqvist ForwardLine3-C Point ForwardLine3-RW Thompson ForwardLine3-RW Gourde ForwardLine1-C Stastny ForwardLine1-C Stamkos ForwardLine1-LW Tarasenko ForwardLine1-LW Namestnikov ForwardLine2-LW Schwartz ForwardLine2-LW Killorn ForwardLine4-LW Upshall ForwardLine4-LW Kunitz DefensePair3-L Bortuzzo DefensePair3-L Sustr DefensePair1-L Edmundson DefensePair1-L Girardi DefensePair2-R Parayko DefensePair2-R Sergachev DefensePair3-R Dunn DefensePair3-R Coburn ForwardLine2-RW Jaskin ForwardLine2-RW Callahan DefensePair1-R Pietrangelo DefensePair1-R Hedman DefensePair2-L Gunnarsson DefensePair2-L Stralman Goalie-Starter Allen Goalie-Starter Vasilevskiy

There should be another 2 lineups that come out but I am either over writing them or my range is not functioning. No errors occur just not the results expected.

EDIT AFTER SUGGESTION:

import base64
import requests
import json
import csv

USERNAME, PASSWORD = 'Name', 'Pass'

header = ["Update", "EX_homepos", "EX_homename", "EX_awaypos", "EX_awayname"]
headers={
            "Authorization": "Basic " +
                    base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
                                    .encode('utf-8')).decode('ascii')
        }

with open("TESTNNEW2.csv", 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    for i in range(41244,41246):
        req = requests.get(url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/game_startinglineup.json?gameid=" + str(i) , headers=headers)
        req.raise_for_status()
        data = req.json()

        my_data =[]
        for i in data:
            Update = data['gamestartinglineup']['lastUpdatedOn']

            for i in range(2):
                EX_team_home = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']

            for i in range(1):
                EX_team_away = data['gamestartinglineup']['teamLineup'][i]['expected']['starter']

            for i in range(20):
                EX_homepos = EX_team_home[i]['position']
                EX_awaypos = EX_team_away[i]['position']
                EX_homename = EX_team_home[i]['player']['LastName']
                EX_awayname = EX_team_away[i]['player']['LastName']

                my_data.append([Update, EX_homepos, EX_homename, EX_awaypos, EX_awayname])

                print(my_data)
                writer.writerows(my_data)

You are ignoring all but the last request by not indenting the code into the loop

I suggest you rewrite the code to open the CSV first, though. You can make a request, parse it, then write the row. There's no benefit to keeping a list of data around for all previous requests

For example

header = ["Updated", "EX_homepos", "EX_homename", "EX_awaypos", "EX_awayname"]
headers = {
        "Authorization": "Basic " +
                base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
                                .encode('utf-8')).decode('ascii')
    }

with open("TESTLINE3.csv", 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(header)
    for gameid in range(41244,41247):
        req = requests.get(url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/game_startinglineup.json?gameid=" + str(gameid) , headers=headers)
        req.raise_for_status()
        data = req.json()
        staring_lineup = data['gamestartinglineup']

        update = staring_lineup['lastUpdatedOn']
        team_away = staring_lineup['teamLineup'][0]['expected']['starter']
        team_home = staring_lineup['teamLineup'][1]['expected']['starter']

        for i in range(20):
            home_player_name = team_home[i]['player']['LastName']
            home_player_pos = team_home[i]['position']

            away_player_name = team_away[i]['player']['LastName']
            away_player_pos = team_away[i]['position']                

            writer.writerow([update, home_player_pos, home_player_name, away_player_pos, away_player_name])

And you don't need close the file when you use with to open the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM