使用django发送http请求并获取结果

send http request using django and get results

这似乎是一个非常简单和愚蠢的问题,但我找不到方便的答案。
我正在尝试将 google 的反向地理编码 api 与 django 一起使用,正如网站所解释的那样(在 https://developers.google.com/maps/documentation/geocoding/start?csw=1#ReverseGeocoding)我应该向 url 发送请求:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=<_api_key>
但是我找不到使用 django 将此请求发送到 google api.
的合适方法 对我来说,似乎一些如此简单的事情可以通过一些内置的 django 方法来实现,但我所能找到的只是要安装的模块。
如果没有办法,只能安装其他 python 模块,哪个最好?

嗯,最直接的方法是安装 requests 库并简单地调用:

result = requests.get(your_link)

您可以在图书馆的 docs. The requests library is very well written, very intuitive and simple to use. Quite a lot of smart people and companies use it, too, so it's not a half-baked library someone just hacked out over the weekend (as of this moment, it has 6060 commits and 595 contributors on GitHub 中找到更多信息(例如,如何进行身份验证、使用 cookie 等,以及如何访问响应数据)。

如果您绝对必须避免使用外部库,为什么不尝试 urllib.request. It's a bit more complicated, and even the docs themselves recommend using requests if you prefer higher-level interface. But you definitely can get the job done with it. To get started, you can read the docs on how to use it to fetch data. Read this 作为示例,了解如何从使用 urllib.[=19 获得的响应中提取 json =]