This article describes how to log on to Discuz through python! Share the common code of the Forum. For more information, see the following code:
# Coding: gbkimport urllib2, urllib, cookielib, and re''' common DZ login Forum parameter descriptions parms: username: User Name (required), password: password (required ), domain: the website domain name. Note that the format must be: http://www.xxx.xx/ (Required), answer: answer to the question, questionid: Question ID, referer: Jump address variable keyword parameters are used here (for more information, see the manual) '''def login_dz (** parms): # initialize parms_key = ['domain ', 'answer', 'Password', 'questionid', 'Referer ', 'username'] arg ={} for key in parms_key: if key in parms: arg [key] = parms [key] else: arg [key] = ''# cookie setting cookieFile = '. /kan_cookies.dat 'cookie = cookielib. LWPCookieJar () opener = urllib2.build _ opener (urllib2.HTTPCookieProces Sor (cookie) # Get formhash pre_login = arg ['domain '] + 'member. php? Mod = logging & action = login & infloat = yes & handlekey = login & inajax = 1 & ajaxtarget = fwin_content_login 'C = opener. open (pre_login ). read () cookie. save (cookieFile) patt = re. compile (R '. *? Name = "formhash ".*? Value = "(.*?) ".*? ') Formhash = patt. search (c) if not formhash: raise Exception ('get formhash Fail! ') Formhash = formhash. group (1) # log on to postdata = {'answer': arg ['answer'], 'formhash': formhash, 'Password': arg ['Password'], 'questionid': 0 if arg ['questionid'] = ''else arg ['questionid'], 'Referer ': arg ['domain '] if arg ['Referer'] = ''else arg ['Referer'], 'username': arg ['username'],} postdata = urllib. urlencode (postdata) req = urllib2.Request (url = arg ['domain '] + 'member. php? Mod = logging & action = login & loginsubmit = yes & handlekey = login & loginhash = LCaB3 & inajax = 1', data = postdata) c = opener. open (req ). read (300) flag = 'login failed % s' % arg ['username'] if 'succeedhandle _ login' in c: flag = True return flag # example: basic Parameter Login user = 'xxx' pwd = 'xxx' dom =' http://www.discuz.net/ '# Another test Website: http://bbs.bitsCN.com/try : Flag = login_dz (username = user, password = pwd, domain = dom) print (flag) failed t Exception, e: print ('error: ', e)