连续的API调用和错误的请求消息

问题描述 投票:0回答:1

我想为'title'中的每个元素进行API调用。

'title'看起来像这样,非常大:

ABMC-2017-0002

ACF-2015-0001

PIN-2017-0004

ADV-2017-0005

AMS-DA-14-0095

VA-2017-VHA-0026

VETS-2017-0001

我想像这样过去:

try:
    for i in title:
        docketID=i

        url=APIUrl+'docket.json?api_key='+APIkey+'&docketId='+docketID
        response = json.load(urlopen(url).read())
        print('json loaded from URL \n')
        time.sleep(3)

except:
    print json.loads(urlopen(url)).get('errors')[0]

在我不断收到HTTP错误400:错误请求后,'time.sleep(3)'被添加。每小时的API调用是有限的。但之后我仍然收到HTTP错误504:网关超时。有谁知道如何循环列表并进行连续的API调用?

python api python-requests bad-request
1个回答
0
投票

我试过你用不同的title代码,它的工作原理

我在页面上的例子中找到了标题'EPA-HQ-OAR-2013-0602'

https://regulationsgov.github.io/developers/basics/

所以你的问题似乎错了标题。或许你以错误的方式使用for循环。您应该显示URL以查看它并在Web浏览器中进行测试。

顺便说一句:如果你使用except Exception, ex: print 'error:', ex然后你会知道你有.read()的其他问题

from urllib2 import urlopen
import json
import time

all_titles = [
    'EPA-HQ-OAR-2013-0602'
]

api_key  = 'PB36zotwgisM02kED1vWwvf7BklqCObDGVoyssVE'
api_base = 'https://api.data.gov/regulations/v3/'

api_url = '{}docket.json?api_key={}&docketId='.format(api_base, api_key)

try:
    for title in all_titles:
        url = api_url + title
        print 'url:', url

        response = urlopen(url) #.read() # <--- Exception
        data = json.load(response)

        print '--- data ---'
        print data
        print '--- keys ---'
        for key in data.keys():
            print 'key:', key

except Exception, ex:
    print 'error:', ex
    print json.loads(urlopen(url)).get('errors')[0]

结果:

url: https://api.data.gov/regulations/v3/docket.json?api_key=PB36zotwgisM02kED1vWwvf7BklqCObDGVoyssVE&docketId=EPA-HQ-OAR-2013-0602
--- data ---    
{u'unfundedmandates': {u'value': u'No', u'label': u'Unfunded Mandates'}, u'energyeffects': {u'value': u'Yes', u'label': u'Energy Effects'}, u'publicationPeriod': {u'value': u'Fall 2015', u'label': u'Publication Period'}, u'timeTables': ...
--- keys ---
key: unfundedmandates
key: energyeffects
key: publicationPeriod
key: timeTables
key: federalismImplications
key: impactsAndEffects
key: rin
key: agendaStageOfRulemaking
key: keywords
key: legalAuthorities
key: publicationperiod
key: relatedDockets
key: energyEffects
key: includedinregulatoryplan
key: title
key: generic
key: internationalImpacts
key: agency
key: smallEntitiesAffected
key: ruleMakingStage
key: priority
key: majorRule
key: agendastageofrulemaking
key: subtype
key: type
key: legalauthorities
key: cfrCitation
key: shorttitle
key: requiresRegulatoryFlexibilityAnalysis
key: internationalimpacts
key: governmentlevelsaffected
key: agencyAcronym
key: requiresregulatoryflexibilityanalysis
key: docketId
key: legaldeadlines
key: unfundedMandates
key: numberOfComments
key: federalismimplications
key: includedInRegulatoryPlan
key: majorrule
key: legalDeadlines
key: smallentitiesaffected
key: governmentLevelsAffected
key: docketAbstract
key: contact
© www.soinside.com 2019 - 2024. All rights reserved.