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