0
我正尝试对Yelp的Fusion API进行API调用。我的电话在硬编码时工作。我试图获得企业名单,然后获得需要两个GET的企业的评论清单。我想浏览一下企业名单并获得相关评论。使用变量表单时,以下代码导致Send a complete request to the server
消息。硬编码商业ID值工作正常。不知道挑战是什么。 (新手问题,所以我的代码可能不是最好的)对Python使用可变的URL值HTTPConnection.request失败
import http.client
import json
conn = http.client.HTTPSConnection("api.yelp.com")
headers = {
'authorization': "Bearer <access token value>",
'cache-control': "no-cache",
'postman-token': "<token value>"
}
#This request works fine
conn.request("GET", "/v3/businesses/search?latitude=40.8059518&longitude=-73.9657435&limit=10&radius=200&term=restaurant", headers=headers)
res = conn.getresponse()
data = res.read()
yelp_result = json.loads(data.decode("utf-8"))
all_businesses = []
for business in yelp_result['businesses']:
b_name = business['name']
b_id = business['id']
rurl = "/v3/businesses/" + b_id + "/reviews"
#This is the request resulting in error given earlier
conn.request("GET",rurl,headers=headers)
all_businesses.append((b_id, b_name))
部分因此,您正在针对*完全不同的*变量网址测试一个硬编码的网址,并且您不确定是哪里出了问题。因此,请一步一步地简化代码,打印出URL,在浏览器中测试它们等。这对我们来说不是问题,您只需要调试它。 –
你可以发布'b_id'和'rurl'的例子吗? –
感谢@JohnZwinck提示和输入。使用建议可以知道conn.getresponse()调用不喜欢在没有相应的conn.getresponse()和res.read()调用的情况下使用。不完全确定为什么,但它的工作原理。通过在工作版本中注释掉这些行并重现以前的错误进行验证。 (似乎很奇怪的IMO,但无论如何)。看到别人正在寻找关于在Python中使用Yelp API的另一个问题的帮助,所以将解决方案稍微进一步化为了希望它也适用于这个问题。再次感谢。 – lmckeogh