使用到的庫: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())

登陸成功
二、健康打卡

點擊提交之后Fiddler會抓到一個POST包

用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':''#空
}
查看post包的raw,得到post的地址,
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/260635.html
標籤:其他
