#encoding=gbk
import sys import re import cookielib import urllib2 import urllib import md5
class Discuz(object):
def __init__(self): self.name=self.pwd=self.operate="" self.cj=cookielib.CookieJar() self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) urllib2.install_opener(self.opener) self.opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)')]
def add_user_info(self,username,pwd): self.name=username self.pwd=pwd
def login(self): m = md5.new() m.update(self.pwd) self.pwd=m.hexdigest() loginpage = self.opener.open('').read() loginpage = self.opener.open('').read() formhash = re.findall('', loginpage.decode("gbk"), re.DOTALL)
params = { 'formhash':formhash[0], 'username':self.name, 'password':self.pwd, 'loginsubmit':'true' } print 'login......' self.request=urllib2.Request('&loginsubmit=yes&inajax=1',urllib.urlencode(params))
try: req = self.opener.open(self.request) page = req.read() if page.find("欢迎"): print "login sucess!" except Exception,e: print e print urllib2.HTTPError def open(self,url): try: page = urllib2.urlopen(url).read() print page except Exception,e: print e if __name__=='__main__': ren=Discuz() ren.add_user_info("your username",'your password') ren.login() ren.open()
|
折腾了一下午终于能登录了,各种低级错误。
本python程序适用于自动登录discuz7.2版本的bbs论坛
阅读(6269) | 评论(0) | 转发(0) |