python - 从终端调用时,对 Foursquare API 的请求不产生任何输出
问题描述
python findArestaurant.py
当我在终端中运行时,以下代码不会产生任何输出
它应该产生一个列出世界各地餐馆的输出。
我注册了一个 Foursqure 开发帐户,创建了一个应用程序,然后能够复制客户端 ID 和客户端密码。但是,没有输出?
文件名:findArestaurant.py
from geocode import getGeocodeLocation
import json
import httplib2
import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
foursquare_client_id = "entered_here"
foursquare_client_secret = "entered_here"
def findARestaurant(mealType,location):
#1. Use getGeocodeLocation to get the latitude and longitude coordinates of the location string.
latitude, longitude = getGeocodeLocation(location)
#2. Use foursquare API to find a nearby restaurant with the latitude, longitude, and mealType strings.
#HINT: format for url will be something like https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=20130815&ll=40.7,-74&query=sushi
url = ('https://api.foursquare.com/v2/venues/search?client_id=%s&client_secret=%s&v=20130815&ll=%s,%s&query=%s' % (foursquare_client_id, foursquare_client_secret,latitude,longitude,mealType))
h = httplib2.Http()
result = json.loads(h.request(url,'GET')[1])
if result['response']['venues']:
#3. Grab the first restaurant
restaurant = result['response']['venues'][0]
venue_id = restaurant['id']
restaurant_name = restaurant['name']
restaurant_address = restaurant['location']['formattedAddress']
address = ""
for i in restaurant_address:
address += i + " "
restaurant_address = address
#4. Get a 300x300 picture of the restaurant using the venue_id (you can change this by altering the 300x300 value in the URL or replacing it with 'orginal' to get the original picture
url = ('https://api.foursquare.com/v2/venues/%s/photos?client_id=%s&v=20150603&client_secret=%s' % ((venue_id,foursquare_client_id,foursquare_client_secret)))
result = json.loads(h.request(url, 'GET')[1])
#5. Grab the first image
if result['response']['photos']['items']:
firstpic = result['response']['photos']['items'][0]
prefix = firstpic['prefix']
suffix = firstpic['suffix']
imageURL = prefix + "300x300" + suffix
else:
#6. if no image available, insert default image url
imageURL = "http://pixabay.com/get/8926af5eb597ca51ca4c/1433440765/cheeseburger-34314_1280.png?direct"
#7. return a dictionary containing the restaurant name, address, and image url
restaurantInfo = {'name':restaurant_name, 'address':restaurant_address, 'image':imageURL}
print ("Restaurant Name: %s" % restaurantInfo['name'] )
print ("Restaurant Address: %s" % restaurantInfo['address'] )
print ("Image: %s \n" % restaurantInfo['image'] )
return restaurantInfo
else:
print ( "No Restaurants Found for %s" % location )
return "No Restaurants Found"
if __name__ == '__main__':
findARestaurant("Pizza", "Tokyo, Japan")
findARestaurant("Tacos", "Jakarta, Indonesia")
findARestaurant("Tapas", "Maputo, Mozambique")
findARestaurant("Falafel", "Cairo, Egypt")
findARestaurant("Spaghetti", "New Delhi, India")
findARestaurant("Cappuccino", "Geneva, Switzerland")
findARestaurant("Sushi", "Los Angeles, California")
findARestaurant("Steak", "La Paz, Bolivia")
findARestaurant("Gyros", "Sydney, Australia")
解决方案
推荐阅读
- gradle - Gradle sourceSets 资源
- scala - 如果它是某个字符串 Spark Scala,则更改列值
- c - libusb:对于 MTP 和 PTP 设备,libusb_kernel_driver_active() 函数总是返回 0?
- java - 我想用使用子字符串返回一个格式化的电话号码,但奇怪的是返回
- flutter - 我的 TextFormField 的验证总是抛出一个错误
- javascript - 垂直滚动时如何阻止水平滑动功能(在手机上)
- css - Tailwind:从绿色到红色取决于使用 Tailwind css 的百分比
- python - 在 Python 中本地安装模块到脚本
- javascript - 在 csv 单元格中写入换行符(\n)?
- node.js - AWS DocumentDB 错误:客户端网络套接字在建立安全 TLS 连接之前断开