【发布时间】:2018-01-19 03:42:30
【问题描述】:
我一直在为计算机科学 30 门课程做作业。在这个作业中,我被要求访问一个 API 并返回一个特定的值,而不仅仅是整个字典。我的问题是我似乎无法打印我想要的值,因为它是一个字符串而不是整数或切片。如果您决定向我提供帮助,这是我为您提供的完整代码。感谢您的宝贵时间。
import requests
import urllib.parse
# set up a temporary copy of the google api
temp = "https://maps.googleapis.com/maps/api/directions/json?
origin=Disneyland&destination=Universal+Studios+Hollywood4"
# set up the main API and the origin and destination inputs from the user
main_api = "https://maps.googleapis.com/maps/api/directions/json?"
origin = str(input("Enter in the place that is you are going away from: "))
destination = str(input("Enter the destination that you would like to reach:
"))
# set up the url from the main api, the origin, and the destination
url = main_api + urllib.parse.urlencode({"origin": origin, "destination":
destination})
# print the url back to the user
print(url)
# show the user the distance between the origin and the destination
json_data = requests.get(url).json()
print(json_data['routes']['legs']['distance']['text'])
【问题讨论】:
-
嗨,您正在寻找这样的东西吗?stackoverflow.com/questions/10399614/…
标签: python api dictionary