Flask 路由未在 Python 上调用 Yelp API
Flask route is not calling Yelp API on Python
所以我在我的索引页面上创建了这条路由,它从 HTML 表单中获取 POST 并在 API 调用中输入变量。我认为 for 循环的初始问题是遍历字典,但根据 yelp 上的 API 用法,似乎 API 甚至没有被调用。有谁知道为什么这条路线不能正常工作?谢谢
@app.route("/",methods=["POST", "GET"])
def zipsub():
global gym_df
if request.method == "POST" and 'zipsearch' in request.form:
zip_code = request.form.get['zipsearch']
#Client ID
clientid = 'client_id'
#define api key, endpoint and header for request to yelp API
api_key = 'api_key'
end_point = 'https://api.yelp.com/v3/businesses/search'
resp_header = {'Authorization': 'bearer {}'.format(api_key)}
#define parameters
parameters = {'term':'gym',
'limit':5,
'radius':3200,
'location':'{}'.format(zip_code),
}
#make api call
response = reqs.get(url=end_point, params=parameters, headers=resp_header)
#Change json into dict then to pandas dataframe
gym_dict = response.json()
for valg in gym_dict['businesses']:
if valg in gym_dict['businesses']:
#only display street address
valg['location']['display_address'] = valg['location']['display_address'][0]
data = valg['image_url'],valg['name'],valg['location']['display_address'],valg['rating'],valg['phone']
datalist = list(data)
seriesly = pd.Series(datalist, index = gym_df.columns)
gym_df = gym_df.append(seriesly, ignore_index=True)
#ORDER DATAFRAME BY RATING
gym_df = gym_df.sort_values(by=['Rating'], ascending=False)
#gym_df.to_html(headers='True', table_id='my_table')
gym_df.to_sql("gym_df", engine, if_exists='append')
return render_template('table.html', tables=gym_df.to_html(table_id='my_table'))
改变了这个:
def zipsub():
global gym_df
if request.method == "POST":
zip_code = request.form.get('zipsearch')
#Client ID
和
return render_template('table.html', column_names=gym_df.columns.values, row_data=list(gym_df.values.tolist()), zip=zip)
此修复让我可以正确地调用和解压缩 API 结果。
所以我在我的索引页面上创建了这条路由,它从 HTML 表单中获取 POST 并在 API 调用中输入变量。我认为 for 循环的初始问题是遍历字典,但根据 yelp 上的 API 用法,似乎 API 甚至没有被调用。有谁知道为什么这条路线不能正常工作?谢谢
@app.route("/",methods=["POST", "GET"])
def zipsub():
global gym_df
if request.method == "POST" and 'zipsearch' in request.form:
zip_code = request.form.get['zipsearch']
#Client ID
clientid = 'client_id'
#define api key, endpoint and header for request to yelp API
api_key = 'api_key'
end_point = 'https://api.yelp.com/v3/businesses/search'
resp_header = {'Authorization': 'bearer {}'.format(api_key)}
#define parameters
parameters = {'term':'gym',
'limit':5,
'radius':3200,
'location':'{}'.format(zip_code),
}
#make api call
response = reqs.get(url=end_point, params=parameters, headers=resp_header)
#Change json into dict then to pandas dataframe
gym_dict = response.json()
for valg in gym_dict['businesses']:
if valg in gym_dict['businesses']:
#only display street address
valg['location']['display_address'] = valg['location']['display_address'][0]
data = valg['image_url'],valg['name'],valg['location']['display_address'],valg['rating'],valg['phone']
datalist = list(data)
seriesly = pd.Series(datalist, index = gym_df.columns)
gym_df = gym_df.append(seriesly, ignore_index=True)
#ORDER DATAFRAME BY RATING
gym_df = gym_df.sort_values(by=['Rating'], ascending=False)
#gym_df.to_html(headers='True', table_id='my_table')
gym_df.to_sql("gym_df", engine, if_exists='append')
return render_template('table.html', tables=gym_df.to_html(table_id='my_table'))
改变了这个:
def zipsub():
global gym_df
if request.method == "POST":
zip_code = request.form.get('zipsearch')
#Client ID
和
return render_template('table.html', column_names=gym_df.columns.values, row_data=list(gym_df.values.tolist()), zip=zip)
此修复让我可以正确地调用和解压缩 API 结果。