Python循环遍历Url Range

时间:2017-12-14 02:59:12

标签: python csv python-requests

下面的脚本应该转到3个不同的页面,并将所有这三个页面写入csv。然而,它只写了最后一页。如果我print(my_data)它也只打印最后一页。这告诉我的是,不是写入csv这是错误,而是在for i in range(41244,41246)内。

这是我的代码:

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

应该有另外两个阵容出来但是我要么写了它们,要么我的范围不起作用。没有错误发生,而不是预期的结果。

建议后编辑:

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)

1 个答案:

答案 0 :(得分:2)

您忽略了除最后一个请求之外的所有请求,而不是将代码缩进到循环中

我建议您先重写代码以打开CSV。您可以发出请求,解析它,然后写入行。保留所有先前请求的数据列表没有任何好处

例如

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])

当您使用with打开文件时,您不需要关闭文件