使用Python HTTPConnection.request的可变URL值失败

时间:2017-08-27 00:23:29

标签: python-3.x yelp

我正在尝试对Yelp的Fusion API进行API调用。我的电话在硬编码时工作。我想获得一个企业列表,然后获得需要两个GET的那些企业的评论列表。我想逐步查看商家列表并获取相关评论。使用变量形式时,以下代码会生成Send a complete request to the server消息。对业务ID值进行硬编码工作正常。不确定挑战是什么。 (新手问题所以我的代码可能不是最好的)

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))

1 个答案:

答案 0 :(得分:0)

conn.request来电的挑战似乎还需要相应的conn.getresponse()res.read()来电。如果不想用它做任何事情,不喜欢打开连接。 (如果有人对此有更深刻的理由,会喜欢它)。以下是如何拨打电话以获取位置半径范围内的有限数量的业务(纬度/经度),然后还将审核片段作为业务信息的一部分(未在新的Fusion 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=(',', ': ')))