[BangPypers] automate logging
pythonic at gmail.com
Wed Jul 23 12:05:22 CEST 2008
leo davis wrote:
> I'm trying to write a code to automate logging into a website.I have
> chosen the forum 'www.tek-tips.com' as an example and tried this
> script on it....But it doesnt seem to work...What am i missing
> here..plz help
> import urllib2
> theurl = 'www.tek-tips.com'
> protocol = 'http://'
> username = 'johnny'
> password = 'yesyes'
> passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
> # this creates a password manager
> passman.add_password(None, theurl, username, password)
> # because we have put None at the start it will always
> # use this username/password combination for urls
> # for which `theurl` is a super-url
> authhandler = urllib2.HTTPBasicAuthHandler(passman)
> # create the AuthHandler
> opener = urllib2.build_opener(authhandler)
> # All calls to urllib2.urlopen will now use our handler
> # Make sure not to include the protocol in with the URL, or
> # HTTPPasswordMgrWithDefaultRealm will be very confused.
> # You must (of course) use it when fetching the page though.
> pagehandle = urllib2.urlopen(protocol + theurl)
> # authentication is now handled automatically for us
> print the_page
Are you sure it uses http basic auth?
I usually use and recommend following packages for web aumtoation scripts.
* twill http://twill.idyll.org/python-api.html
* BeautifulSoup http://www.crummy.com/software/BeautifulSoup
More information about the BangPypers