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")
But all I can find using Google are httplib
and urllib
- and I am unable to find a shortcut in those libraries.
Does standard Python 2.5 have a shortcut in some form as above, or should I write a function url_get
?
- I would prefer not to capture the output of shelling out to
wget
orcurl
.
theller's solution for wget is really useful, however, i found it does not print out the progress throughout the downloading process. It's perfect if you add one line after the print statement in reporthook.
If you want solution with httplib2 to be oneliner consider instantiating anonymous Http object
Excellent solutions Xuan, Theller.
For it to work with python 3 make the following changes
Also, the URL you enter should be preceded by a "http://", otherwise it returns a unknown url type error.
Python 2.x:
Python 3.x:
Documentation for urllib.request and read.
How is that?
Here is a wget script in Python:
If you are working with HTTP APIs specifically, there are also more convenient choices such as Nap.
For example, here's how to get gists from Github since May 1st 2014:
More examples: https://github.com/kimmobrunfeldt/nap#examples