Requests is good for times when you just need to fetch a webpage and do something with the raw HTML. It doesn't give you a whole lot more, but does do that incredibly well! The Requests homepage has lots of good examples and full documentation.
In [1]:
import requests
r = requests.get('http://www.imdb.com/name/nm0000125/')
You can make sure the request actually worked (ie. HTTP status code 200)
In [2]:
r.status_code
Out[2]:
You can check what type of content the webpage returned (ie. text, json, csv, etc)
In [3]:
r.headers['content-type']
Out[3]:
You can check the character set (sure hope it is utf-8!)
In [18]:
r.encoding
Out[18]:
Of course, you can get the actual HTML text too!
In [4]:
r.text[0:200]
Out[4]:
But sometimes you just want the header content, for instance if you want to resolve redirects without actually downloading the full webpage content.
In [5]:
r = requests.head("http://feeds.foxnews.com/~r/foxnews/national/~3/vZ_mHFtNHag/", allow_redirects=True)
r.url
Out[5]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: