简单健康打卡爬虫脚本

使用到的库: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)
  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值