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

时间:2017-12-30 01:07:00

标签: python api python-requests bad-request

我想为'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错误400:错误请求后被添加。每小时的API调用是有限的。但之后我仍然收到HTTP错误504:网关超时。 有谁知道如何循环列表并进行连续的API调用?

1 个答案:

答案 0 :(得分: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