【问题标题】:HERE requests how to assign multiple values to a key in paramsHERE 请求如何为 params 中的键分配多个值
【发布时间】:2021-02-21 18:31:20
【问题描述】:

我正在尝试使用 FindSequence API 为 n 个目的地获取最佳路线。这是我的代码:

URL5 = "https://wse.ls.hereapi.com/2/findsequence.json"
params5 = {
    "apiKey": my_key,
    "departure":"2014-11-11T16:52:00Z",
    "start": first,
    "destinations": [i for i in f], 
    "end": last,
    "mode": "fastest;truck;traffic:enabled" # To enable traffic, "departure" parameter must be included
}

res5 = requests.get(url = URL5, params = params5)
data5 = res5.json()

其中first 和last 是带有目的地名称的字符串;lat,long

  • first = "HARDIE'S MOBILE HOME PARK; 30.44126118, -86.6240656099999"
  • last = "HOUSTON PARK MOBILE HOME PARK; 30.4424195300001, -86.64733076"

而f 是一个列表,其中包含路线在到达last 目的地之前必须经过的其他点。

f = ['CRESTVIEW RV PARK; 30.7190163500001, -86.5716222299999',
 'HOMESTEAD TRAILER PARK; 30.5115772500001, -86.4628417499999']

但是,它看起来只是“读取”first 和 last 数据点,并且完全忽略了列表 f 中的其他两个目的地。这是我得到的输出:

[{'id': "HARDIE'S MOBILE HOME PARK",
  'lat': 30.44126118,
  'lng': -86.6240656099999,
  'sequence': 0,
  'estimatedArrival': None,
  'estimatedDeparture': '2014-11-11T16:52:00Z',
  'fulfilledConstraints': []},
 {'id': 'HOUSTON PARK MOBILE HOME PARK',
  'lat': 30.4424195300001,
  'lng': -86.64733076,
  'sequence': 1,
  'estimatedArrival': '2014-11-11T17:02:30Z',
  'estimatedDeparture': None,
  'fulfilledConstraints': []}]

所需的输出必须如下所示:

[{'id': "HARDIE'S MOBILE HOME PARK",
  'lat': 30.44126118,
  'lng': -86.6240656099999,
  'sequence': 0,
  'estimatedArrival': None,
  'estimatedDeparture': '2014-11-11T16:52:00Z',
  'fulfilledConstraints': []},
 {'id': 'CRESTVIEW RV PARK',
  'lat': 30.719016,
  'lng': -86.571622,
  'sequence': 1,
  'estimatedArrival': None,
  'estimatedDeparture': '2014-11-11T17:37:08Z',
  'fulfilledConstraints': []},
 {'id': 'HOMESTEAD TRAILER PARK',
  'lat': 30.511577,
  'lng': -86.462842,
  'sequence': 2,
  'estimatedArrival': None,
  'estimatedDeparture': '2014-11-11T18:07:25Z',
  'fulfilledConstraints': []},
 {'id': 'HOUSTON PARK MOBILE HOME PARK',
  'lat': 30.4424195300001,
  'lng': -86.64733076,
  'sequence': 3,
  'estimatedArrival': '2014-11-11T18:43:24Z',
  'estimatedDeparture': None,
  'fulfilledConstraints': []}]

并且可以使用以下代码获得所需的输出:

URL3 = "https://wse.ls.hereapi.com/2/findsequence.json"
params3 = {
    "apiKey": here_api_key,
    "departure":"2014-11-11T16:52:00Z",
    "start": first,
    "destination1": "CRESTVIEW RV PARK;30.719016, -86.571622",
    "destination2": "HOMESTEAD TRAILER PARK;30.511577, -86.462842",
    "end": last,
    "mode": "fastest;truck;traffic:enabled" # To enable traffic, "departure" parameter must be included
}

res3 = requests.get(url = URL3, params = params3)
data3 = res3.json()

虽然上面的代码运行良好,但这不是最好的方法,因为我需要从两个目的地通过。我是 HERE API 的新手,所以如果有人知道该怎么做,我将非常感谢您的帮助!

【问题讨论】:

    标签: python python-requests here-api here-fleet-telematics


    【解决方案1】:

    您可以在FindSequence API documentation 中验证没有destinations 参数之类的东西,因此在您的第一个示例中,路线没有访问这些目的地是完全可以理解的。所以必须传递destination1、destination2等参数。

    如果您想在您的字典中自动创建这些参数,因为我不是 Python 用户,请在 StackOverflow 中搜索以了解如何,这些答案有很多可以帮助您,例如:

    programmatically generate key and values to put in python dictionary

    Create dictionary where keys are variable names

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 2021-04-08
      • 2020-03-01
      • 1970-01-01
      • 2021-05-12
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多