在 Python 中 HTTP GET 的最快方法是什么? [英] What is the quickest way to HTTP GET in Python?
本文介绍了在 Python 中 HTTP GET 的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我知道内容将是一个字符串,那么在 Python 中进行 HTTP GET 的最快方法是什么?我正在搜索文档以获取快速的单行代码,例如:
What is the quickest way to HTTP GET in Python if I know the content will be a string? I am searching the documentation for a quick one-liner like:
contents = url.get("http://example.com/foo/bar")
但是我可以使用 Google 找到的只有 httplib
和 urllib
- 我无法在这些库中找到快捷方式.
But all I can find using Google are httplib
and urllib
- and I am unable to find a shortcut in those libraries.
标准 Python 2.5 是否有上述某种形式的快捷方式,或者我应该编写一个函数 url_get
?
Does standard Python 2.5 have a shortcut in some form as above, or should I write a function url_get
?
- 我不想将炮击输出捕获到
wget
或curl
.
推荐答案
Python 3:
import urllib.request
contents = urllib.request.urlopen("http://example.com/foo/bar").read()
Python 2:
import urllib2
contents = urllib2.urlopen("http://example.com/foo/bar").read()
这篇关于在 Python 中 HTTP GET 的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文