使用循环在Python中迭代,直到到达列表的末尾

时间:2022-03-16 21:23:05

I am working on a project where I want to pull long lists of data from an API (Strava) and put them in a MySQL database (not shown in code). There are some limitations to the API and they don't allow to pull more than 200 records per request. Therefore, I need to find a way to iterate over the results, but to stop when the end of the list is reached (It never returns completely empty and does not error out either, no matter how many pages I pull).

我正在开发一个项目,我想从API(Strava)中提取长数据列表并将它们放在MySQL数据库中(未在代码中显示)。 API存在一些限制,它们不允许每个请求提取超过200条记录。因此,我需要找到一种迭代结果的方法,但是当到达列表的末尾时停止(它永远不会返回完全为空,也不会出错,无论我拉多少页)。

So, I defined a function that pulls the data from the API and the first key in the JSON output is the effort_count which equals the number of entries in the list. I try to use that number from the function in the loop that I built to iterate over the list using the function.

因此,我定义了一个从API中提取数据的函数,JSON输出中的第一个键是effort_count,它等于列表中的条目数。我尝试使用我构建的循环中的函数中的该数字来使用该函数迭代列表。

import urllib2
import json 

strava_api='xxxxxxx'
def strava(segment_id,page,per_page):

    access_token = strava_api
    url='https://www.strava.com/api/v3/segments/' + str(segment_id) +'/leaderboard' +'?&access_token=' + access_token       

    final_url=url + "&page=" + str(page) + "&per_page=" + str(per_page)
    json_obj=urllib2.urlopen(final_url)

    data = json.load(json_obj) 
    effort_count=data['effort_count']

counter = 1
max_page = effort_count/200+1
for counter in range (1,max_page):
    strava(894142,counter,200) # 894142 is an existing segment and 200 is the max number of records displayed
    counter = counter + 1

However, when I run this, I get the error that 'effort_count' is not defined. Is this because it is not possible to use a variable from a function in this loop? Is there a workaround?

但是,当我运行它时,我收到'effort_count'未定义的错误。这是因为在这个循环中不可能使用函数中的变量吗?有解决方法吗?

ps I am quite new at coding, so it is very well possible I am taking the wrong approach entirely.

ps我在编码方面很陌生,所以我很可能完全采用错误的方法。

1 个解决方案

#1


0  

Indeed effort_count is not defined at the global scope.

实际上,在全球范围内没有定义effort_count。

You could remedy this by declaring it global at the start of your method e.g.

您可以通过在方法开始时将其声明为全局来解决此问题,例如:

def strava(segment_id,page,per_page):
    global effort_count

    access_token = strava_api

But you're still left with the problem that effort_count is accessed before your method is invoked.

但是,在调用方法之前,仍然存在访问effort_count的问题。

You should also initialise it to some sensible default value at the start of your script.

您还应该在脚本开头将其初始化为一些合理的默认值。

#1


0  

Indeed effort_count is not defined at the global scope.

实际上,在全球范围内没有定义effort_count。

You could remedy this by declaring it global at the start of your method e.g.

您可以通过在方法开始时将其声明为全局来解决此问题,例如:

def strava(segment_id,page,per_page):
    global effort_count

    access_token = strava_api

But you're still left with the problem that effort_count is accessed before your method is invoked.

但是,在调用方法之前,仍然存在访问effort_count的问题。

You should also initialise it to some sensible default value at the start of your script.

您还应该在脚本开头将其初始化为一些合理的默认值。