Python Login Script -
i know similar questions have been asked, have read them. have read other related articles find. have tried httplib2, header modifications, , else find or think of, cannot login script 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 open "%s".' % theurl if hasattr(e, 'code'): print 'we failed error code - %s.' % e.code elif hasattr(e, 'reason'): print "the error object has following 'reason' attribute :", e.reason print "this means server doesn't exist, down, or don't have internet connection." sys.exit() else: print 'here headers of page :' print handle.info()
first not script have modified some. second think has way cookies handled can't figure out. replaced username password combination.
i suppose it's again term of service should try
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 = {'referer': 'http://www.biocidecity.com/index.php'} 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 open "%s".' % theurl if hasattr(e, 'code'): print 'we failed error code - %s.' % e.code elif hasattr(e, 'reason'): print "the error object has following 'reason' attribute :", e.reason print "this means server doesn't exist, down, or don't have internet connection." sys.exit() else: print 'here headers of page :' print handle.info()
Comments
Post a Comment