模拟教务处登录(验证码以下载图片方式人工处理)

#-*- coding:utf-8 -*-
import string 
import urllib.request
import os
from PIL import Image
import urllib.parse
import http.cookiejar


url="http://jw.sdufe.edu.cn/Logon.do?method=logon"

cookie =  http.cookiejar.CookieJar()
hander = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(hander)

print('请输入您的学号')#201690040191
username=input()
print('请输入您的密码')#1015mjw.
userpass=input()

#判断文件是否存在
bcwjpd=os.path.exists(r"C:\Users\CheneyPang\Desktop\yzm")
if bcwjpd:
    print("文件已存在")
else:
    os.mkdir(r"C:\Users\CheneyPang\Desktop\yzm")


#下载验证码到本地
url_yzm="http://jw.sdufe.edu.cn/verifycode.servlet"
#response=urllib.request.urlopen("http://jw.sdufe.edu.cn/verifycode.servlet")
#data=response.read()
data=opener.open(url_yzm).read()
filename=r"C:\Users\CheneyPang\Desktop\yzm\yam.png"
f=open(filename,"wb")
f.write(data)
f.close()
print("保存文件完成")

im=Image.open(r"C:\Users\CheneyPang\Desktop\yzm\yam.png")
im.show()

print('请输入验证码')
code=input()


PostData = {
    'userAccount':username,#'这里填写学号
    'userPassword':userpass,#这里填写密码
    'RANDOMCODE':code,
    #'encoded':'2B0A61566bh69101090F4x30H221f179HV101%Y%o%J81uW0dal1j507Lm6jw.'
}

headers ={
    'Accept':'text/html,application/xhtml+xm,application/xml;q=0.9,*/*;q=0.8',
    'Connection':'keep-alive',
    'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
    'Content-Type':'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0;win64;x64;rv:64.0) Gecko/20100101 Firefox/64.0'
}

#PostData['RANDOMCODE']=code
#PostData['encoded']='2B0A61566bh69101090F4x30H221f179HV101%Y%o%J81uW0dal1j507Lm6jw.'


data=urllib.parse.urlencode(PostData).encode(encoding='utf-8')
request=urllib.request.Request(url,data,headers)
response=opener.open(request)
result=response.read().decode('utf-8')

print(result)
#res=opener.open()#打开子网页

 

你可能感兴趣的:(Python.爬虫)