python - 带有python请求库的zomato api请求

标签 python json python-requests pycurl zomato-api

Zomato 是最受欢迎的餐厅搜索引擎之一,提供免费的 api 服务...

如果在api请求中使用curl,效果完美;

curl -X GET --header "Accept: application/json" --header "user_key: MY_API_KEY_HERE" "https://developers.zomato.com/api/v2.1/geocode?lat=41.10867962215988&lon=29.01834726333618"

但是使用了Python的requests库,是不行的。当我执行下面的代码时;

import requests
r = requests.get("https://developers.zomato.com/api/v2.1/geocode?lat=41.10867962215988&lon=29.01834726333618", headers={"user_key": "MY_API_KEY_HERE", "Accept": "application/json"});

解释器返回以下错误;

requests.exceptions.ProxyError: Cannot connect to proxy. Socket error: Tunnel connection failed: 403 Forbidden.

通过 pyCurl 库进行了多次尝试,但不幸的是结果是一样的; 403 禁止

我该如何解决这个问题?

最佳答案

我在使用 Zomato API 时也遇到了问题。我遇到了 500 服务器错误

在 header 中添加 User Agent 信息解决了我的问题。

import requests
from pprint import pprint

locationUrlFromLatLong = "https://developers.zomato.com/api/v2.1/cities?lat=28&lon=77"
header = {"User-agent": "curl/7.43.0", "Accept": "application/json", "user_key": "YOUR_API_USER_KEY"}

response = requests.get(locationUrlFromLatLong, headers=header)

pprint(response.json())

关于python - 带有python请求库的zomato api请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34038673/

相关文章:

javascript - 使用远程 JSON 填充 HTML5 本地存储

python - 在 Airflow 中的所有 DAG 中控制对同一 API 的请求(具有请求限制)的最佳方法是什么

python - 如何在误差线旁边添加误差值?

python - 来自 scikits cross_val_score 的所有类别的 f1 分数

jquery - 来自 LinkedIn 和 StumbleUpon 的 JSONP 请求

python - 在 Dropbox API Python 上实现 OAuth

python - 递归处理分页

python - 为什么 numpy 在字符串转换时会截断数字?

python - Numpy longdouble 算术似乎不在 long double with conversion

java - Toast 数组值不起作用