python爬蟲之獲取驗證碼登陸


#--coding:utf-8
#author:wuhao
#
#這里我演示的就是本人所在學校的教務系統
#
import urllib.request
import urllib.parse
import re
import shutil
import http.cookiejar

class LoginJust():
def __init__(self,url,url1,url2,header,account,pwd):
self.url=url
self.url1=url1
self.url2=url2
self.header=header
self.account=account
self.pwd=pwd
return

#創建opener,包含header信息和cookie
def CreateOpener(self):
#實例化cookie對象
cookie=http.cookiejar.CookieJar()
#創建一個cookie處理器
CookieHandle=urllib.request.HTTPCookieProcessor(cookie)
#創建帶有cookie的opener
opener=urllib.request.build_opener(CookieHandle)
#傳入header
head=[]
for key,value in self.header.items():
elem=(key,value)
head.append(elem)
opener.open(self.url)
return opener

#獲取驗證碼圖片,並保存到本地
def getImage(self,opener):
path = "imageCode_1.jpg"
# 帶cookie和header
img = opener.open(self.url2)
with open(path, "wb") as f:
shutil.copyfileobj(img, f)
#print(os.stat(path).st_size, 'characters copied')
return

#獲取后台發送來的字符串
def getResponse(self,opener):
getReponse = opener.open(self.url1)
#
gRResult = getReponse.read().decode("utf-8")
return gRResult

#獲取post所需的encoded參數
def achieveCode(self,response):
# scode、sxh是response中得來
scode = response.split("#")[0]
sxh = response.split("#")[1]
# userAccount、userPassword
userAccount = self.account
userPassword =self.pwd
code = userAccount + "%%%" + userPassword
# 最終需要獲取的post的數據
encode = ""
# 進行賬號密碼加密,獲取code的值
i = 0
while (i < len(code)):
if i < 20:
encode = encode + code[i:i + 1] + scode[0:int(sxh[i])]
# print(str(i)+"_encode:"+encode)
scode = scode[int(sxh[i]):len(scode)]
# print(str(i)+"_scode:"+scode)
else:
encode = encode + code[i:len(code)]
# print(str(i)+"_here_encode"+encode)
i = len(code)
i = i + 1
return encode

#判斷登陸是否成功
def IsLoginS(self,encoded):
#驗證碼的值
codeValue=input("請輸入驗證碼:")
data={"useDogCode":"","encoded":encoded,"RANDOMCODE":codeValue}
#把data轉換為post的數據格式
postData=urllib.parse.urlencode(data)
result=opener.open(self.url,postData.encode("utf-8"))
Result=result.read().decode("utf-8")
regex=re.compile(r"1440407133")
#print(type(regex.search(Result)))
if regex.search(Result)!=None:
return True
else:
print("Someting Error happened:登陸失敗")
return False

#再次請求驗證碼


# 需要post的網址的URL
url = "http://jwgl.just.edu.cn:8080/Logon.do?method=logon"
# 獲取后台數據的網址
url1 = "http://jwgl.just.edu.cn:8080/Logon.do?method=logon&flag=sess"
# 獲取驗證碼圖片的地址
url2 = "http://jwgl.just.edu.cn:8080/verifycode.servlet"
#header
header=\
{
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"Referer":"http://jwgl.just.edu.cn:8080/Logon.do?method=logon",
"Host":"jwgl.just.edu.cn:8080",
"Connection":"keep-alive",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.8",
"Upgrade-Insecure-Requests":"1",
"Cache-Control":"max-age=0",
}
account=input("請輸入用戶名:")
pwd=input("請輸入密碼:")
#實例化對象
lj=LoginJust(url,url1,url2,header,account,pwd)
opener=lj.CreateOpener()
lj.getImage(opener)
encoded=lj.achieveCode(lj.getResponse(opener))
if lj.IsLoginS(encoded):
print("登陸成功")

運行結果

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM