使用django发送http请求并获取结果 [英] send http request using django and get results

查看:196
本文介绍了使用django发送http请求并获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常简单和愚蠢的问题,但我找不到方便的答案.
我正在尝试将django与google的反向geocodding api结合使用,正如网站解释的那样(在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模块,那是最好的吗?

This might seem like an extremely simple and stupid question yet i can't find a convenient answer.
I'm trying to use google's reverse geocodding api with django, as the website explains (at https://developers.google.com/maps/documentation/geocoding/start?csw=1#ReverseGeocoding) I'm supposed to send a request to the url :
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=<_api_key>
But I can't find an appropriate way to send this request using django to google api.
To me it seems something so simple will be doable by some method built-in django but all I could find was modules to install.
If there's no way but installing other python modules which one's the best?

推荐答案

最简单的方法是安装 requests 库,只需调用:

Well, the most straightforward way would be to install requests library and simply call:

result = requests.get(your_link)

您可以在图书馆的

You can find additional information (e.g., how to authenticate, use cookies, etc., as well as how to access data in response) in the library's 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 5917 commits and 554 contributors on GitHub).

如果您绝对必须避免使用外部库,为什么不尝试 docs 有关如何使用它来进行获取的信息数据.阅读此线程作为示例如何从urllib响应中提取json.

If you absolutely must avoid external libraries, why not try 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 thread for an example of how to extract json from a response you'd get with urllib.

这篇关于使用django发送http请求并获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆