authentication - Get content of protected web pages using Python -
how can content of protected pages using python , urllib2?
i need specify username , password pages trying retrieve.. e.g.
content = urllib2.urlopen(url, username, password).read() i know not part of urllib2 api.. give example of need, api.
i suggest @ python requests library.
it has great support basic http authentication out of box.
e.g.
import requests content = requests.get(url, auth=('user', 'pass')) using requests can set sessions (for cookie management) , post data (e.g. login form) , keep cookie browse pages accessible logged in users.
read more session objects , posting data in excellent documentation.
if absolutely have use urllib2 here's useful snippet taken thread basic http authentication:
import urllib2, base64 request = urllib2.request("http://api.foursquare.com/v1/user") base64string = base64.standard_b64encode('%s:%s' % (username, password)) request.add_header("authorization", "basic %s" % base64string) result = urllib2.urlopen(request)
Comments
Post a Comment