I know similar questions have been asked, I have read them. I have also read most other related articles I could find. I have tried httplib2, header modifications, and anything else I could find or think of, however I cannot get this login script to work.
import cookielib
import urllib
import urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
resp = opener.open('http://www.biocidecity.com')
theurl = 'http://www.biocidecity.com/index.php'
body={'username':'someusername','password':'somepassword', 'login' : '1'}
txdata = urllib.urlencode(body) txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
try:
req = urllib2.Request(theurl, txdata, txheaders)
handle = opener.open(req)
HTMLSource = handle.read()
f = file('test.html', 'w')
f.write(HTMLSource)
f.close()
except IOError, e:
print 'We failed to open "%s".' % theurl
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
elif hasattr(e, 'reason'):
print "The error object has the following 'reason' attribute :", e.reason
print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
sys.exit()
else:
print 'Here are the headers of the page :'
print handle.info()
First this is not my script however I have modified it some. Second I think it has something to do with the way the cookies are handled but I can't figure it out. I also replaced the username password combination.