使用到的库:requests、urllib、http
先用Fiddler抓包。
在浏览器中先登陆一遍。
在Fiddler中会抓到POST
查看POST的body
import requests
from urllib import request,parse
import urllib
构造发送头
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68',
'Connection':'keep-alive'
}
构造发送表单
value = {
'Login.Token1':User,
'Login.Token2':Password,
'goto':'http://my.hhu.edu.cn/loginSuccess.portal',
'gotoOnFail':'http://my.hhu.edu.cn/loginFailure.portal'
}
在之前抓取的post包里的raw里得到登陆URL:
URL = "http://ids.hhu.edu.cn/amserver/UI/Login"
准备登陆:
import http.cookiejar, urllib.request
firename = "cookie.txt"
cookie = http.cookiejar.MozillaCookieJar(firename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
登陆:
postdata = urllib.parse.urlencode(value).encode()
req = urllib.request.Request(URL, postdata, headers)
保存cookie并打印
try:
response = opener.open(req)
except urllib.error.URLError as e:
print(e.reason)
cookie.save(ignore_discard=True, ignore_expires=True)
for item in cookie:
print('Name =' + item.name)
print('Value =' + item.value)
可以查看一个需要登陆之后才能访问的界面验证是否登陆成功
get_request = urllib.request.Request('http://my.hhu.edu.cn/index.portal',headers=headers)
get_response = opener.open(get_request)
print(get_response.read().decode())
用post包的body,创建一个表单。
根据每天时间不同改变时间,使用time库
from time import strftime
time = strftime("%Y/%m/%d")
values_submit = {
#时间
'DATETIME_CYCLE':time,
#学号
'XGH_336526':'186xxxxxxx',
#姓名
'XM_1474':'xx',
#身份证号码
'SFZJH_859173':'xxxxxxxxxxxxxxxx',
#院系
'SELECT_941320':'机电院',
#年级
'SELECT_459666':'2018级',
#专业
'SELECT_814855':'机械',
#班级
'SELECT_525884':'机械18_1',
#宿舍楼
'SELECT_125597':'常州校区16号楼',
#宿舍号
'TEXT_950231':'107',
#手机号
'TEXT_937296':'159611270xx',
'RADIO_853789':'否',
'RADIO_43840':'否',
'RADIO_579935':'健康',
'RADIO_138407':'否',
'RADIO_546905':'否',
'RADIO_314799':'否',
'RADIO_209256':'否',
'RADIO_836972':'否',
'RADIO_302717':'否',
'RADIO_701131':'否',
'RADIO_438985':'否',
'RADIO_467360':'是',
#家庭住址
'PICKER_956186':'山西省,太原市,小店区',
'TEXT_434598':'',#空
'TEXT_515297':'',#空
'TEXT_752063':''#空
}
URL_POST = 'http://form.hhu.edu.cn/pdc/formDesignApi/dataFormSave?wid=A335B048C8xxxxxxxxxxxx101600A6A04&userId=186xxxxxxx'#去掉了数据
发送post包
postdata = urllib.parse.urlencode(values_submit).encode()
req = urllib.request.Request(URL_POST, postdata,headers)
response = opener.open(req)
全部代码
import requests
from urllib import request,parse
import urllib
from time import strftime
URL = "http://ids.hhu.edu.cn/amserver/UI/Login"
URL_POST = 'http://form.hhu.edu.cn/pdc/formDesignApi/dataFormSave?wid=A335B048C8456F75E0538101600A6A04&userId=1861010125'#应该每个人wid不一样
User = "1xxxxxxxx"
Password = "xxxxxxx"
Name = 'xx'
StuID = '1861xxxxxx'
IDs = '1xxxxxxxxxxxxxxxxxxx'
Institute = '机电院'
Grade = '2018级'
Major = '机械'
ClassID = '机械18_x'
Building = '常州校区xx号楼'
DormitoryID = 'xxx'
PhoneNub = '159611270xx'
Home = '山西省,太原市,小店区'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68',
'Connection':'keep-alive'
}
value = {
'Login.Token1':User,
'Login.Token2':Password,
'goto':'http://my.hhu.edu.cn/loginSuccess.portal',
'gotoOnFail':'http://my.hhu.edu.cn/loginFailure.portal'
}
time = strftime("%Y/%m/%d")
values_submit = {
'DATETIME_CYCLE':time,
'XGH_336526':StuID,
'XM_1474':Name,
'SFZJH_859173':IDs,
'SELECT_941320':Institute,
'SELECT_459666':Grade,
'SELECT_814855':Major,
'SELECT_525884':ClassID,
'SELECT_125597':Building,
'TEXT_950231':DormitoryID,
'TEXT_937296':PhoneNub,
'RADIO_853789':'否',
'RADIO_43840':'否',
'RADIO_579935':'健康',
'RADIO_138407':'否',
'RADIO_546905':'否',
'RADIO_314799':'否',
'RADIO_209256':'否',
'RADIO_836972':'否',
'RADIO_302717':'否',
'RADIO_701131':'否',
'RADIO_438985':'否',
'RADIO_467360':'是',
'PICKER_956186':Home,
'TEXT_434598':'',
'TEXT_515297':'',
'TEXT_752063':''
}
# 保存cookie到本地
import http.cookiejar, urllib.request
firename = "cookie.txt"
cookie = http.cookiejar.MozillaCookieJar(firename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
#登陆
postdata = urllib.parse.urlencode(value).encode()
req = urllib.request.Request(URL, postdata, headers)
try:
response = opener.open(req)
except urllib.error.URLError as e:
print(e.reason)
# 保存cookie
cookie.save(ignore_discard=True, ignore_expires=True)
for item in cookie:
print('Name =' + item.name)
print('Value =' + item.value)
#使用cookie健康打卡
postdata = urllib.parse.urlencode(values_submit).encode()
req = urllib.request.Request(URL_POST, postdata,headers)
response = opener.open(req)
代码在ubuntu服务器上运行时出现了一些小问题,python获取的时间比现实时间慢一些,可能有时会出现日期差错。所以脚本中用pytz库设置了时区,再进行时间的获取。
import requests
from urllib import request,parse
import urllib
from time import strftime
import pytz
import datetime
import http.cookiejar, urllib.request
URL = "http://ids.hhu.edu.cn/amserver/UI/Login"
。。。。。。。。。。相同。。。。。。。。。。。
value = {
'Login.Token1':User,
'Login.Token2':Password,
'goto':'http://my.hhu.edu.cn/loginSuccess.portal',
'gotoOnFail':'http://my.hhu.edu.cn/loginFailure.portal'
}
#######################时区修改和时间获取###################
tz = pytz.timezone('Asia/Shanghai')
time = datetime.datetime.now(tz).strftime('%Y/%m/%d')
#########################################################
values_submit = {
。。。。。。。。。
后面的相同
response = opener.open(req)