Python-使用While循环,如何更新要在函数中使用的变量字符串

问题描述

背景:我正在尝试使用从Twitch的API调用的数据创建一个数据框。他们每次呼叫仅允许100条记录,因此每次拉动都会提供一个新的分页光标,以转到下一页。我正在使用以下代码尝试并有效地提取此数据,而不是在get响应中手动调整after =(分页值)。现在,我要使之动态的变量是'Pagination'变量,但仅在循环结束后才更新-毫无用处!请在下面查看,看看您是否注意到我可以更改以实现此目标的任何内容。任何帮助表示赞赏!

TwitchTopGamesDataFrame = [] #This is our Data List                    
BaseURL = 'https://api.twitch.tv/helix/games/top?first=100'
Headers = {'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization': 'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
Indent = 2
Pagination = ''
FullURL = BaseURL + Pagination
Response = requests.get(FullURL,headers=Headers)
iterations = 1 # Data records returned are equivalent to iterations x100

#Loop: Response,Convert JSON data,Append to Data List,Get Pagination & Replace String in Variable - Iterate until 300 records
while count <= 3:
    #Grab JSON Data,Convert,& Append
    ResponseJSONData = Response.json()
    #print(pgn) - Debug
    pd.set_option('display.max_rows',None)
    TopGamesDF = pd.DataFrame(ResponseJSONData['data'])
    TopGamesDF = TopGamesDF[['id','name']]
    TopGamesDF = TopGamesDF.rename(columns={'id':'GameID','name':'GameName'})
    TopGamesDF['Rank'] = TopGamesDF.index + 1
    TwitchTopGamesDataFrame.append(TopGamesDF)
    #print(FullURL) - Debug
    #Grab & Replace Pagination Value
    ResponseJSONData['pagination']
    RPagination = pd.DataFrame(ResponseJSONData['pagination'],index=[0])
    pgn = str('&after='+RPagination.to_string(index=False,header=False).strip())
    Pagination = pgn
    #print(FullURL) - Debug
    iterations += 1
TwitchTopGamesDataFrame```

解决方法

想通了:

def top_games(page_count):
    from time import gmtime,strftime
    strftime("%Y-%m-%d %H:%M:%S",gmtime())
    print("Time of Execution:",strftime("%Y-%m-%d %H:%M:%S",gmtime()))

    #In order to condense the code above and be more efficient,a while/for loop would work great.
    #Goal: Run a While Loop to create a larger DataFrame through Pagination as the Twitch API only allows for 100 records per call.

    baseURL = 'https://api.twitch.tv/helix/games/top?first=100' #Base URL
    Headers = {'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization': 'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
    Indent = 2
    Pagination = ''
    FullURL = BaseURL + Pagination
    Response = requests.get(FullURL,headers=Headers)
    start_count = 0
    count = 0 # Data records returned are equivalent to iterations x100
    max_count = page_count

    #Loop: Response,Convert JSON data,Append to Data List,Get Pagination & Replace String in Variable - Iterate until 300 records
    while count <= max_count:
        #Grab JSON Data,Extend List
        Pagination
        FullURL = baseURL + Pagination
        Response = requests.get(FullURL,headers=Headers)
        ResponseJSONData = Response.json()
        pd.set_option('display.max_rows',None)
        if count == start_count:
            TopGamesDFL = ResponseJSONData['data']
        if count > start_count:
            i = ResponseJSONData['data']
            TopGamesDFL.extend(i)
        #Grab & Replace Pagination Value
        ResponseJSONData['pagination']
        RPagination = pd.DataFrame(ResponseJSONData['pagination'],index=[0])
        pgn = str('&after='+RPagination.to_string(index=False,header=False).strip())
        Pagination = pgn
        count += 1        
        if count == max_count:
            FinalDataFrame = pd.DataFrame(TopGamesDFL)
            FinalDataFrame = FinalDataFrame[['id','name']]
            FinalDataFrame = FinalDataFrame.rename(columns={'id':'GameID','name':'GameName'})
            FinalDataFrame['Rank'] = FinalDataFrame.index + 1
    return FinalDataFrame

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...