栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法使用Python遍历分页的API响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法使用Python遍历分页的API响应

问题似乎是:

  • 您在第一个电话中就获得了补偿,但对此电话返回的实际公司数据不做任何事情。
  • 然后,您可以在while循环中使用相同的偏移量;您永远不会在后续通话中使用新的。这就是为什么您每次都会得到相同的公司的原因。

我认为此代码

get_companies()
适合您。显然,我无法对其进行测试,但希望可以:

def get_companies():        create_get_recent_companies_call = "https://api.hubapi.com/companies/v2/companies/?hapikey={hapikey}".format(hapikey=wta_hubspot_api_key)        headers = {'content-type': 'application/json'}        create_get_recent_companies_response = requests.get(create_get_recent_companies_call, headers=headers)        if create_get_recent_companies_response.status_pre == 200: while True:     for i in create_get_recent_companies_response.json()[u'companies']:         companyName = i[u'properties'][u'name'][u'value']         print("{companyName}".format(companyName=companyName))     offset = create_get_recent_companies_response.json()[u'offset']     hasMore = create_get_recent_companies_response.json()[u'has-more']     if not hasMore:         break     else:         create_get_recent_companies_call = "https://api.hubapi.com/companies/v2/companies/?hapikey={hapikey}&offset={offset}".format(hapikey=wta_hubspot_api_key, offset=offset)         create_get_recent_companies_response = requests.get(create_get_recent_companies_call, headers=headers)        else: print("Something went wrong, check the supplied field values.n") print(json.dumps(create_get_recent_companies_response.json(), sort_keys=True, indent=4))

严格来说,不需要

else
after,
break
但与Python
Zen
保持一致:“显式优于隐式”

请注意,您只需要检查一次200响应代码,如果循环内出现问题,您将错过它。您可能应该将所有调用放入循环中,并每次都检查是否有正确的响应。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号