2017-08-27 2 views
0

私はYelpのFusion APIへのAPI呼び出しを試みています。私の呼び出しは、ハードコードされたときに機能します。私はビジネスのリストを取得しようとしているし、2つのGETを必要とするビジネスのためのレビューのリストを取得しています。私はビジネスのリストを歩み、関連するレビューを取得したいと思います。変数フォームを使用すると、次のコードではSend a complete request to the serverというメッセージが表示されます。ビジネスID値をハードコーディングしても問題ありません。挑戦が何であるか分かりません。 (初心者の質問がそう私のコードは、どちらかおそらく最高ではありません)conn.requestコールとPython HTTPConnection.requestに可変URL値を使用できません

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を完全に異なる*変数URLに対してテストしています。何が間違っているのかは分かりません。一歩一歩進み、コードを単純化し、URLをプリントアウトし、ブラウザでテストしてください。これは疑問ではなく、単にデバッグするだけです。 –

+0

'b_id'と' rurl'の例を投稿できますか? –

+0

ヒントと入力のための@JohnZwinckに感謝します。アドバイスを使用することで、conn.getresponse()呼び出しが対応するconn.getresponse()およびres.read()呼び出しなしで使用したくないことを知ることができました。なぜそれがうまくいくのかは分かりません実用バージョンでこれらの行をコメントアウトし、以前のエラーを再現することで確認しました。 (奇妙なIMOに見えるが、何でも)。他の誰かがPythonでYelp APIを使用することについての別の質問で助けを求めていたので、解決策をもう少し考えて、その質問に対しても適用したいと考えていました。再度、感謝します。 – lmckeogh

答えて

0

チャレンジが、それはまた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=(',', ': '))) 
関連する問題