从Python脚本查询远程API

时间:2016-04-20 20:06:17

标签: python api

我想在我的Python脚本中集成Zomato的{{1}}。

我知道Python中的基本编程,但对于API集成,我想知道

  • 如何继续使用API​​
  • 应该采取什么步骤来从Python查询API
  • 如何将结果集成到我的脚本中

有哪些可行的选择?

2 个答案:

答案 0 :(得分:1)

您可以开始使用Python的requests模块。

获取类别列表的快速示例:

import requests

domain = "https://developers.zomato.com/api/v2.1"
headers = {'user-key': 'your_api_key_here'}

response = requests.get("{}/categories".format(domain), headers=headers).json()

for category in response["categories"]:
    print(category)

这将输出如下内容:

{"categories": {"id": 1, "name": "Delivery"}}
{"categories": {"id": 2, "name": "Dine-out"}}
{"categories": {"id": 3, "name": "Nightlife"}}
{"categories": {"id": 4, "name": "Catching-up"}}
{"categories": {"id": 5, "name": "Takeaway"}}
# etc...

requests can be found here.

的文档

答案 1 :(得分:0)

你的问题可能过于宽泛,不属于这个答案的范围,但我会尽我所能。

所有这些链接都将为您提供大量信息,以帮助您了解如何使用Zomato API完全满足您的需求。您可以做的最好的事情是自己了解HTTP请求的工作方式,然后按照这些链接开始。