2017-08-27 37 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=(',', ': ')))