网址参数我做错了什么?

时间:2017-02-21 17:19:44

标签: python api parameters

我试图从RIDB API(政府娱乐数据)获取信息。但是,我对如何使用URL参数感到困惑。我查了一些例子并尝试了很多不同的东西。

首先,开始我https://ridb.recreation.gov/api/v1/trails/USFS/工作得很好。我得到一个包含跟踪信息的JSON结果,非常完美。但是,如果我尝试https://ridb.recreation.gov/api/v1/trails/USFS/milesmin10https://ridb.recreation.gov/api/v1/trails/USFS/milesmin=10https://ridb.recreation.gov/api/v1/trails/USFS?milesmin=10https://ridb.recreation.gov/api/v1/trails/USFS?milesmin10作为示例,我什么也得不回来。从附件中可以看到milesmin是一个可以接受的参数,有人可以解释如何在我的代码中包含这些参数和/或我明显做错了什么。

URL Parameters

1 个答案:

答案 0 :(得分:1)

我使用requests,它运行正常。您必须注册their API key才能使用此服务。这个过程很快,我在不到5分钟的时间内获得了一把钥匙。这是获得结果的代码。

import requests

# You must replace <your api key> with the key they give you
api = <your api key>
url = 'https://ridb.recreation.gov/api/v1/trails/USFS/'

params = {'milesmin': 10, 'apikey': api}

r = requests.get(url, params=params)


#    In [168]: r.request.url
#    Out[168]: 'https://ridb.recreation.gov/api/v1/trails/USFS/?milesmin=10&apikey=<your api key>'

#    In [169]: r.text[:500]
#    Out[169]: u'{"RECDATA":[{"TypicalTreadWidth":"N\\/A","MinimumTrailWidth":"N\\/A","TypicalTreadCrossSlope
#    ":"N\\/A","TerraMotorized":"N\\/A","AllowedSnowUse":"N\\/A","PackSaddleRestricted":"N\\/A",
#    "TrailSurface":"N\\/A","TrailNo":"8021SA","AllowedTerraUse":"N\\/A","XcountrySkiAccptDisc":"N\\/A",
#    "TrailUSFSID":20,"NonmotorWatercraftManaged":"N\\/A","XcountrySkiManaged":"N\\/A",
#    "BicycleManaged":"N\\/A","ShapeLength":0.5105766578900838,"MVUMSymbol":99,
#    "MotorcycleManaged":"N\\/A","GEOM":"LINESTRING (-111.69189874799997 39.9735'