Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python API未返回正确的信息_Python_Json_Api_Python Requests - Fatal编程技术网

Python API未返回正确的信息

Python API未返回正确的信息,python,json,api,python-requests,Python,Json,Api,Python Requests,我正在看这个API: 以下请求 在浏览器中打开,将返回一个json结构,其中包含汉堡和柏林之间的距离,以及许多与这些城市和它们之间的路线相关的附加信息 所以我写这段代码只是为了测试它: import json import requests url = 'http://www.distance24.org/route.json?stops=Hamburg|Berlin' info = requests.request("GET", url) info = json.loa

我正在看这个API:

以下请求

在浏览器中打开,将返回一个json结构,其中包含汉堡和柏林之间的距离,以及许多与这些城市和它们之间的路线相关的附加信息

所以我写这段代码只是为了测试它:

import json
import requests
url = 'http://www.distance24.org/route.json?stops=Hamburg|Berlin' 
info = requests.request("GET", url)
info = json.loads(info.text) 
print(info['distance'])
打印结果是0,这绝对不是从汉堡到柏林的距离


我对此有点陌生,所以可能遗漏了一些明显的东西。

这不是使用
请求的方式

试试这个:

导入请求
端点=”https://www.distance24.org/route.json?stops=Hamburg|柏林“
打印(requests.get(endpoint.json()[“distance”])
输出:

256
Hamburg, officially Free and Hanseatic City of Hamburg, is the second-largest city in Germany, the thirteenth largest German state, and the sixth-largest city in the European Union. The city is home to over 1.8 million people, while the Hamburg Metropolitan Region has more than 4.3 million inhabitants. Situated on the river Elbe, the port of Hamburg is the second largest port in Europe (after the Port of Rotterdam) and 11th-largest worldwide.
下面是如何获得汉堡摘要,例如:

import requests

endpoint = "https://www.distance24.org/route.json?stops=Hamburg|Berlin"
response = requests.get(endpoint).json()

hamburg_abstract = response["stops"][0]["wikipedia"]["abstract"]
print(hamburg_abstract)
输出:

256
Hamburg, officially Free and Hanseatic City of Hamburg, is the second-largest city in Germany, the thirteenth largest German state, and the sixth-largest city in the European Union. The city is home to over 1.8 million people, while the Hamburg Metropolitan Region has more than 4.3 million inhabitants. Situated on the river Elbe, the port of Hamburg is the second largest port in Europe (after the Port of Rotterdam) and 11th-largest worldwide.

这不是您应该如何使用
请求

试试这个:

导入请求
端点=”https://www.distance24.org/route.json?stops=Hamburg|柏林“
打印(requests.get(endpoint.json()[“distance”])
输出:

256
Hamburg, officially Free and Hanseatic City of Hamburg, is the second-largest city in Germany, the thirteenth largest German state, and the sixth-largest city in the European Union. The city is home to over 1.8 million people, while the Hamburg Metropolitan Region has more than 4.3 million inhabitants. Situated on the river Elbe, the port of Hamburg is the second largest port in Europe (after the Port of Rotterdam) and 11th-largest worldwide.
下面是如何获得汉堡摘要,例如:

import requests

endpoint = "https://www.distance24.org/route.json?stops=Hamburg|Berlin"
response = requests.get(endpoint).json()

hamburg_abstract = response["stops"][0]["wikipedia"]["abstract"]
print(hamburg_abstract)
输出:

256
Hamburg, officially Free and Hanseatic City of Hamburg, is the second-largest city in Germany, the thirteenth largest German state, and the sixth-largest city in the European Union. The city is home to over 1.8 million people, while the Hamburg Metropolitan Region has more than 4.3 million inhabitants. Situated on the river Elbe, the port of Hamburg is the second largest port in Europe (after the Port of Rotterdam) and 11th-largest worldwide.

这是一个API问题,而不是Python编程。根据API,结果是0@TusharNirasAPI很好。这是OP如何使用
请求
。这是一个API问题,而不是Python编程。根据API,结果是0@TusharNirasAPI很好。这是OP如何使用
请求
。谢谢!我使用请求的方式有什么问题?什么时候使用requests.request()函数合适?我强烈建议您阅读
请求
,了解更多信息。此外,如果你觉得答案有用,请查看此链接,谢谢!我使用请求的方式有什么问题?什么时候使用requests.request()函数合适?我强烈建议您阅读
请求
,了解更多信息。此外,如果你发现答案有用,请检查此项