2017-08-27 33 views
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)) 
+0

部分因此,您正在针对*完全不同的*变量网址测试一个硬编码的网址,并且您不确定是哪里出了问题。因此,请一步一步地简化代码,打印出URL,在浏览器中测试它们等。这对我们来说不是问题,您只需要调试它。 –

+0

你可以发布'b_id'和'rurl'的例子吗? –

+0

感谢@JohnZwinck提示和输入。使用建议可以知道conn.getresponse()调用不喜欢在没有相应的conn.getresponse()和res.read()调用的情况下使用。不完全确定为什么,但它的工作原理。通过在工作版本中注释掉这些行并重现以前的错误进行验证。 (似乎很奇怪的IMO,但无论如何)。看到别人正在寻找关于在Python中使用Yelp API的另一个问题的帮助,所以将解决方案稍微进一步化为了希望它也适用于这个问题。再次感谢。 – lmckeogh

回答

0

挑战与conn.request呼吁似乎是它也需要相应的conn.getresponse()res.read()电话。如果不打算做任何事情,不喜欢打开连接。 (如果有人对此有更深刻的理由,会喜欢它)。以下是如何拨打电话的位置(经/纬)半径范围内获得的企业数量有限,而且随后还包括评价摘要作为商家信息(新的融合API的不退换)

#My version of pulling a Yelp review around a particular business 
#3 step proceedure 
# 1. find all businesses around a location 
# 2. convert to a dictionary with the key = business ID in Yelp 
# 3a. request the reviews for the business ID, iteratively if necessary 
# 3b. add reviews to the appropriate business as a list[0:2] of dict 

import http.client 
import json 
# Step 1. Yelp API call to return list of businesses around a location 
# for example, hard coded lat/long values used in conn.request call below. 
# Future functions: 
# - dynamically create desired location. 
# - number of businesses to return, current limit is 5 for testing 
headers = { 
    'authorization': "Bearer <access token value>", 
    'cache-control': "no-cache", 
    'postman-token': "<token value>" 
} 
conn = http.client.HTTPSConnection("api.yelp.com") 
conn.request("GET", "/v3/businesses/search?latitude=40.8059518&longitude=-73.9657435&limit=5&radius=200&term=restaurant", headers=headers) 
res = conn.getresponse() 
data = res.read() 
yelp_result = json.loads(data.decode("utf-8")) #converts byte data to just text 

# Step 2. convert to a dictionary with keys = business ID values 
# Think the for loop below can be simplified. (Future effort) 
biz2 = {} 
for business in yelp_result['businesses']: 
    b_id = business['id'] 
    biz2[b_id] = business 

# Step 3. Request and adding reviews to appropriate business 
for business in yelp_result['businesses']: 
    b_id = business['id'] 
    r_url = "/v3/businesses/" + b_id + "/reviews" #review request URL creation based on business ID 
    #Step 3a. request the reviews for the business ID 
    conn.request("GET",r_url,headers=headers) 
    rev_res = conn.getresponse()  #response and read functions needed else error(?) 
    rev_data = rev_res.read() 
    yelp_reviews = json.loads(rev_data.decode("utf-8")) 
    #Step 3b. add reviews to the appropriate business 
    biz2[b_id]['reviews'] = yelp_reviews['reviews'] 

print(json.dumps(biz2, indent=3, separators=(',', ': ')))