简体   繁体   English

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

[英]send http request using django and get results

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 : 我试图使用谷歌的反向geocodding api与django,因为网站解释(在https://developers.google.com/maps/documentation/geocoding/start?csw=1#ReverseGeocoding )我应该发送一个请求到网址:
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. 但我找不到使用django向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. 对我来说,内置django的方法看起来很简单,但我能找到的只是要安装的模块。
If there's no way but installing other python modules which one's the best? 如果没有办法安装其他python模块哪一个最好?

Well, the most straightforward way would be to install requests library and simply call: 那么,最直接的方法是安装requests库并简单地调用:

result = requests.get(your_link)

You can find additional information (eg, how to authenticate, use cookies, etc., as well as how to access data in response) in the library's docs . 您可以在库的文档中找到其他信息(例如,如何进行身份验证,使用cookie等,以及如何访问响应中的数据)。 The requests library is very well written, very intuitive and simple to use. requests库编写得非常好,非常直观且易于使用。 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 5612 commits and 529 contributors on GitHub ). 相当多的聪明人和公司也使用它,所以它不是一个半生不熟的图书馆,有人刚刚在周末被黑了(截至目前,它在GitHub上有5612个提交和529个贡献者)。

If you absolutely must avoid external libraries, why not try urllib.request . 如果你绝对必须避免使用外部库,为什么不试试urllib.request It's a bit more complicated, and even the docs themselves recommend using requests if you prefer higher-level interface. 它有点复杂,如果您更喜欢更高级别的界面,甚至文档本身也会建议使用requests 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 . 阅读此主题以获取如何从您使用urllib获得的响应中提取json的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM