python模拟登录验证码(代码简单)

废话不多说了,直接给大家贴代码了。

import urllib
import urllib2
import cookielib
def getimg(picurl):
”’
request for random_code picture and cookie
”’
pic = opener.open(picurl).read()
with open(‘./verifyimg.jpg’,’wb’) as emptypic:
emptypic.write(pic)
def verifyimg(picpath):
”’
todo
”’
pass
def login(coo):
loginurl = ‘http://jwxt.wust.edu.cn/whkjdx/logon.do?method=logon’
data = {‘username’:’*****’, ‘password’:’*****’, ‘randomcode’:coo}
#encode the postdata
postdata = urllib.urlencode(data)
user_agent = ‘mozilla/4.0 (compatible; msie 5.5; windows nt)’
header = {‘user-agent’:user_agent,’referer’:’http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp’}
#generate a request with url,postdata headers and cookie
request = urllib2.request(loginurl, postdata, headers = header)
#post data
content = opener.open(request)
#get html file
mainurl = ‘http://jwxt.wust.edu.cn/whkjdx/framework/main.jsp’
maincontent = opener.open(mainurl).read()
print maincontent
if __name__ == ‘__main__’:
cookie = cookielib.cookiejar()
handler = urllib2.httpcookieprocessor(cookie)
opener = urllib2.build_opener(handler)
picurl = ‘http://jwxt.wust.edu.cn/whkjdx/verifycode.servlet’
getimg(picurl)
#verifyimg(picpath)
randomcode = raw_input(‘plz input teh randomcode:’)
login(randomcode)

以上代码大家看起来很容易吧,有不明白的地方欢迎留言,我会在第一时间和大家取得联系的。在此脚本之家小编祝大家新年快乐。

Posted in 未分类

发表评论