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

Consecutive API Calls and Bad Request Messages

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

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

ABMC-2017-0002

ACF-2015-0001

ACUS-2017-0004

ADF-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]

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

我试过你用不同的代码 title 并且有效

我在第

页的示例中找到了标题 'EPA-HQ-OAR-2013-0602'

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

所以你的问题好像标题不对。或者您可能以错误的方式使用了 for 循环。您应该显示 url 以查看它并在网络浏览器中进行测试。

顺便说一句:如果你使用 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