首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在API调用中使用多个参数时,Python中的嵌套循环

在API调用中使用多个参数时,Python中的嵌套循环
EN

Stack Overflow用户
提问于 2018-12-12 19:20:21
回答 2查看 2K关注 0票数 1

我有一个调用API、返回JSON数据并将其写入CSV的程序。该程序作为API调用中的第一个参数循环遍历一个实体列表,但现在还需要遍历第二个参数集(开始时间和结束时间),因为API每次最多要提取一周的数据。

示例:

API调用: ex.com/api/entity/timecards?start_time=1531306800&end_time=1531846800&pretty=1

所以我需要遍历所有的实体,然后遍历一整年的数据,一周一周。

到目前为止,API调用函数的代码示例如下:

代码语言:javascript
复制
 def callAPI(entities):
        for j in range(len(entities)):

           locnum = entities[j][:5]
           locnumv = entities[j]

           startTime =
           endTime = 

           url = "http://ex.com/api/entity/" + entity[j] + "/timecards?start_time=" + startTime + "&end_time=" + endTime

           querystring = {"pretty":"1"}

           headers = {
            'Api-Key': ""
            }

           r = requests.request("GET", url, headers=headers, params=querystring)

           d = r.json()

然后,该程序继续将数据写入CSV中的行,当通过带有静态时间参数的实体进行循环测试时,这一切都是成功的。

因此,我只需要弄清楚如何创建另一个嵌套的for循环来循环开始时间/结束时间+ 518400秒(为了安全起见,这是6天而不是7天),并且考虑到超时,因为这实际上将是20,000+ API调用,在所有这些操作完成之前?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-12 21:52:27

首先,因为您只是使用j来获取当前实体,所以可以用for j in range(len(entities))替换for entity in entities,它读起来更好。至于这个问题,您可以使用一个内部for循环来每周迭代一次。整个守则将是:

代码语言:javascript
复制
def callAPI(entities):
  for entity in entities:
    locnum = entity[:5]
    locnumv = entity # This is redundant

    START = 1531306800  # starting time, 1 year ago
    END = START + 31536000  # final time, e.g. the current time
    TIME_STEP = 518400  # 1 day, 1 week, 1 month

    for start_time in range(START, END, TIME_STEP):
      end_time = start_time + TIME_STEP - 1 # substract 1 for preventing overlap of times

      url = "http://ex.com/api/entity/%s/timecards?start_time=%d&end_time=%d" % (entity, start_time, end_time)

      querystring = {"pretty":"1"}

      headers = {'Api-Key': ""}

      try:
          r = requests.request("GET", url, headers=headers, params=querystring)
      except:
          break

      d = r.json()

      # Do something with the data

我希望这能帮到你!

票数 0
EN

Stack Overflow用户

发布于 2018-12-12 20:53:46

首先,你可以这样做:

代码语言:javascript
复制
for entity in entities:

而不是:

代码语言:javascript
复制
for j in range(len(entities)):

然后使用entity代替entities[j]

当它循环通过你的。您必须设置开始时间,然后在另一个循环中将结束时间设置为start_time + 540000

代码语言:javascript
复制
   start_time = 1531306800
   i = 0
   while True:
       if i != 0:
           start_time = end_time

       end_time = start_time + 540000

       url = "http://ex.com/api/entity/" + entity + "/timecards?start_time=" + start_time + "&end_time=" + end_time

       querystring = {"pretty":"1"}

       headers = {'Api-Key': ""}

       try:
           r = requests.request("GET", url, headers=headers, params=querystring)
       except:
           break

       d = r.json()

基本上,您将遍历所有的请求,直到请求失败为止。一旦完成,您将退出循环并转到您的下一个实体。新实体的url将在与其之前的实体相同的时代开始,依此类推。

我希望这能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53749960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档