'xxx,你怎么沒打卡,你是不是睡過頭了?哈哈哈'
'你還沒打卡分蓖,快去打卡啊,在線等尔许,急么鹤!'
'.............'
來源:本人博客
萌生想法
? 疫情以來每天都要完成健康打卡,說來慚愧味廊,有時(shí)候我真的就忘了蒸甜,還要去監(jiān)督別人。在上次用Python給釣魚網(wǎng)站提交垃圾信息后余佛,我就想著用Python來實(shí)現(xiàn)一鍵提交柠新,然后再配合windos的計(jì)劃任務(wù),每天自動(dòng)提交豈不是美滋滋辉巡。
? 今天` 2020年4月11日 `(過了0點(diǎn))恨憎,我完成了適合自己的腳本。不出意外的話下一個(gè)零點(diǎn)我就可以一鍵上報(bào)了。
看看實(shí)驗(yàn)圖:
這里顯示`今天已經(jīng)填報(bào)過了`是因?yàn)槲抑鲃?dòng)提交了信息憔恳,目的是方便我抓數(shù)據(jù)瓤荔。
一天只能提交一次實(shí)在不方便我抓數(shù)據(jù),其實(shí)昨天有機(jī)會(huì)來著钥组,被我浪費(fèi)了输硝。
發(fā)現(xiàn)問題
? 但是有問題存在
?-?別人怎么用?
?-?怎么登陸程梦?
這個(gè)時(shí)候就要提到`Cookie`了
???什么餅干
Cookie
簡單地說点把,某網(wǎng)站能`'一周免登陸'`就是用到了Cookie
百度百科:
Cookie,有時(shí)也用其復(fù)數(shù)形式 Cookies作烟。類型為“小型文本文件”愉粤,是某些網(wǎng)站為了辨別用戶身份砾医,進(jìn)行Session跟蹤而儲(chǔ)存在用戶本地終端上的數(shù)據(jù)(通常經(jīng)過加密)拿撩,`由用戶客戶端計(jì)算機(jī)暫時(shí)或永久保存的信息`。
我的想法是:要想大家都能用還需要登錄抓取Cookie
`現(xiàn)在2020/4/11 1:35:06 睡覺啦如蚜,明天繼續(xù)`
終于成功
現(xiàn)在`2020/4/12 3:26:23 `我們繼續(xù)
經(jīng)過辣么辣么久的嘗試(剛?cè)腴TPython),我終于成功啦
代碼我會(huì)放在最后压恒,因?yàn)楝F(xiàn)在水平不高,大佬勿噴啊
`記得安裝result庫:pip install result`
?-?`https://app.nwafu.edu.cn/uc/wap/login/check`模擬登錄错邦,保存cookie
?-?`https://app.nwafu.edu.cn/ncov/wap/default/save`再繼續(xù)請(qǐng)求健康上報(bào)頁面
健康上報(bào)頁面的要提交的header包含很多信息探赫,在瀏覽器抓到的數(shù)據(jù)中
?-?`https://webapi.amap.com/maps/ipLocation`是獲取經(jīng)緯度的的請(qǐng)求地址
?-?`https://restapi.amap.com/v3/geocode/regeo`是獲取詳細(xì)地址的請(qǐng)求地址
提交詳細(xì)地址請(qǐng)求后會(huì)返回
```json
jsonp_703113_({"status":"1","regeocode":{"addressComponent":{"city":"某某地區(qū)","province":"新省","adcode":"行政區(qū)號(hào)","district":"縣","towncode":"行政區(qū)號(hào)","streetNumber":{"number":"街道號(hào)","location":"經(jīng)度,緯度","direction":"方向","distance":"34.3624","street":"街道"},"country":"國家","township":"鎮(zhèn)","businessAreas":[[]],"building":{"name":[],"type":[]},"neighborhood":{"name":[],"type":[]},"citycode":"區(qū)號(hào)"},"formatted_address":"詳細(xì)地址"},"info":"OK","infocode":"10000"})
```
在這里我將這串?dāng)?shù)據(jù)賦值給`city0`,然后用字符串切片截取數(shù)據(jù)
```python
city=city0[city0.index('"city":"')+8:city0.index('","province"')]
province=city0[city0.index('"province":"')+12:city0.index('","adcode"')]
district=city0[city0.index('district":"')+11:city0.index('","towncode"')]
adcode=city0[city0.index('"adcode":"')+10:city0.index('","district"')]
citycode=city0[city0.index('"citycode":"')+12:city0.index('"},"formatted_address":')]
```
然后放到`data`中
```python
data = {
??'tw': '7',
??'sfcxtz': '0',
??'sfjcbh': '0',
??'sfcxzysx': '0',
??'qksm': '',
??'sfyyjc': '0',
??'jcjgqr': '0',
??'remark': '',
??'address': address,
??'geo_api_info': geo_api_info,
??'area': area,
??'province': province,
??'city': city,
??'sfzx': '0',
??'sfjcwhry': '0',
??'sfjchbry': '0',
??'sfcyglq': '0',
??'gllx': '',
??'glksrq': '',
??'jcbhlx': '',
??'jcbhrq': '',
??'bztcyy': '1',
??'sftjhb': '0',
??'sftjwh': '0',
??'jcjg': '',
??'uid': '77567',
??'created': '1586448147',
??'date': '20200410',
??'jcqzrq': '',
??'sfjcqz': '',
??'szsqsfybl': '0',
??'sfsqhzjkk': '',
??'sqhzjkkys': '',
??'sfygtjzzfj': '0',
??'gtjzzfjsj': '',
??'id': '2551432',
??'gwszdd': '',
??'sfyqjzgc': '',
??'jrsfqzys': '',
??'jrsfqzfy': '',
??'ismoved': '0'
}
```
最后提交,大工告成
在全部代碼的12行改好自己的學(xué)號(hào)密碼撬呢,然后將這個(gè)腳本添加至計(jì)劃任務(wù)就可以每天`自動(dòng)打卡`了
不知道如何設(shè)置的可以參考:windows創(chuàng)建定時(shí)任務(wù)執(zhí)行python腳本")
全部代碼
```python
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#請(qǐng)?jiān)?2行補(bǔ)全賬號(hào)密碼
import urllib.request
import urllib.parse
#import urllib.errorss
import http.cookiejar
import requests
import time
LOGIN_URL = r'https://app.nwafu.edu.cn/uc/wap/login/check'??# 登錄教務(wù)系統(tǒng)的URL,目的是獲取cookie
get_url = 'https://app.nwafu.edu.cn/ncov/wap/default/save'??# 利用cookie請(qǐng)求打卡地址
values = {'username': '在這里輸入學(xué)號(hào)', 'password': '在這里輸入密碼'}
postdata = urllib.parse.urlencode(values).encode()
############################################################################################################
headers3 = {
????'authority': 'webapi.amap.com',
????'pragma': 'no-cache',
????'cache-control': 'no-cache',
????'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
????'sec-fetch-dest': 'script',
????'accept': '*/*',
????'sec-fetch-site': 'cross-site',
????'sec-fetch-mode': 'no-cors',
????'referer': 'https://app.nwafu.edu.cn/ncov/wap/default/index',
????'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
}
params3 = (
????('key', '729923f88542d91590470f613adb27b5'),
????('callback', 'jsonp_734125_'),
????('platform', 'JS'),
????('logversion', '2.0'),
????('appname', 'https://app.nwafu.edu.cn/ncov/wap/default/index'),
????('csid', '88DB2BE4-AEC3-47E1-909F-E08A63B3CF61'),
????('sdkversion', '1.4.4'),
)
response = requests.get('https://webapi.amap.com/maps/ipLocation', headers=headers3, params=params3)
ipdz=response.text
jdd=ipdz[ipdz.index('"lat":"')+7:ipdz.index('"})')]
print('經(jīng)度:',jdd,end='??')
wdd=ipdz[ipdz.index('"lng":"')+7:ipdz.index('","lat"')]
print('緯度:',wdd)
#以上獲取經(jīng)緯度
############################################################################################################
headers1 = {
????'Connection': 'keep-alive',
????'Pragma': 'no-cache',
????'Cache-Control': 'no-cache',
????'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
????'Sec-Fetch-Dest': 'script',
????'Accept': '*/*',
????'Sec-Fetch-Site': 'cross-site',
????'Sec-Fetch-Mode': 'no-cors',
????'Referer': 'https://app.nwafu.edu.cn/ncov/wap/default/index',
????'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
}
params1 = (
????('key', '729923f88542d91590470f613adb27b5'),
????('s', 'rsv3'),
????('language', 'undefined'),
????('location', '75.86036,39.37025'),
????('extensions', 'base'),
????('callback', 'jsonp_703113_'),
????('platform', 'JS'),
????('logversion', '2.0'),
????('appname', 'https://app.nwafu.edu.cn/ncov/wap/default/index'),
????('csid', '090D6179-CE87-4E0C-8AA9-69B0B3E8FF8A'),
????('sdkversion', '1.4.4'),
)
response1 = requests.get('https://restapi.amap.com/v3/geocode/regeo', headers=headers1, params=params1)
city0=response1.text
fa=city0[city0.index('ess":"')+6:city0.index('"},"info')]
print('位置:',fa)
#以上獲取地址
city=city0[city0.index('"city":"')+8:city0.index('","province"')]
province=city0[city0.index('"province":"')+12:city0.index('","adcode"')]
district=city0[city0.index('district":"')+11:city0.index('","towncode"')]
adcode=city0[city0.index('"adcode":"')+10:city0.index('","district"')]
citycode=city0[city0.index('"citycode":"')+12:city0.index('"},"formatted_address":')]
print(city,end='??')
print(province,end='??')
print(district)
print("行政區(qū)劃代碼:",adcode)
print('區(qū)號(hào):',citycode)
area=province+' '+city+' '+district
address=province+city+district
############################################################################################################
headers = {'authority': 'app.nwafu.edu.cn','pragma': 'no-cache','cache-control': 'no-cache','upgrade-insecure-requests': '1','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36','sec-fetch-dest': 'document','accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','sec-fetch-site': 'same-origin','sec-fetch-mode': 'navigate','referer': 'https://app.nwafu.edu.cn/site/applicationSquare/index?sid=8','accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',}
info0='{"type":"complete","info":"SUCCESS","status":1,"Eia":"jsonp_676131_","position":{"P":'
jd=jdd
wd=wdd
info3='},"message":"Get ipLocation success.Get address success.","location_type":"ip","accuracy":null,"isConverted":true,"addressComponent":{"citycode":'
ac='adcode":"'
info4='"businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"","streetNumber":"","province":"","city":"","district":"","township":""},"formattedAddress":'
info1=',"roads":[],"crosses":[],"pois":[]}'
d=','
info2=','
geo_api_info=info0+jd+d+'"O":'+wd+d+'"lng":'+wd+d+'"lat":'+jd+info3+'"'+citycode+'"'+d+'"'+ac+address+'"'+d+info4+fa+'"'+info1+info2
data = {
??'tw': '7',
??'sfcxtz': '0',
??'sfjcbh': '0',
??'sfcxzysx': '0',
??'qksm': '',
??'sfyyjc': '0',
??'jcjgqr': '0',
??'remark': '',
??'address': address,
??'geo_api_info': geo_api_info,
??'area': area,
??'province': province,
??'city': city,
??'sfzx': '0',
??'sfjcwhry': '0',
??'sfjchbry': '0',
??'sfcyglq': '0',
??'gllx': '',
??'glksrq': '',
??'jcbhlx': '',
??'jcbhrq': '',
??'bztcyy': '1',
??'sftjhb': '0',
??'sftjwh': '0',
??'jcjg': '',
??'uid': '77567',
??'created': '1586448147',
??'date': '20200410',
??'jcqzrq': '',
??'sfjcqz': '',
??'szsqsfybl': '0',
??'sfsqhzjkk': '',
??'sqhzjkkys': '',
??'sfygtjzzfj': '0',
??'gtjzzfjsj': '',
??'id': '2551432',
??'gwszdd': '',
??'sfyqjzgc': '',
??'jrsfqzys': '',
??'jrsfqzfy': '',
??'ismoved': '0'
}
cookie_filename = 'cookie_jar.txt'
cookie_jar = http.cookiejar.MozillaCookieJar(cookie_filename)
handler = urllib.request.HTTPCookieProcessor(cookie_jar)
opener = urllib.request.build_opener(handler)
request = urllib.request.Request(LOGIN_URL, postdata, headers)
try:
????response = opener.open(request)
????# print(response.read().decode())
except urllib.error.URLError as e:
????print(e.code, ':', e.reason)
cookie_jar.save(ignore_discard=True, ignore_expires=True)??# 保存cookie到cookie.txt中
print('*'*80)
print('cookie已經(jīng)寫.py文件同路徑的“cookie_jar.txt”')
print('cookie信息:')
for item in cookie_jar:
????print('name=',item.name,end=' ')
????print('value=',item.value,end=' ')
print('')
print('*'*80)
data=urllib.parse.urlencode(data).encode()
get_request = urllib.request.Request(get_url, headers=headers,data=data)
get_response = opener.open(get_request)
final=get_response.read().decode()
if '成功' in final:
????pirnt("歐耶伦吠!上報(bào)完成")
????print('完成,程序3秒后退出')
????print('3')
????time.sleep(1)
????print('2')
????time.sleep(1)
????print('1')
????time.sleep(1)
????print('Bye bye??:)')
elif '已經(jīng)填報(bào)' in final:
????print("今天已經(jīng)填報(bào)過啦???)QWQ )")
????print('完成魂拦,程序3秒后退出')
????print('3')
????time.sleep(1)
????print('2')
????time.sleep(1)
????print('1')
????time.sleep(1)
????print('Be seeing you??:)')
elif '請(qǐng)輸入' in final:
????print('發(fā)生錯(cuò)誤毛仪,請(qǐng)檢查賬號(hào)密碼,程序3秒后退出')
????print('3')
????time.sleep(1)
????print('2')
????time.sleep(1)
????print('1')
????time.sleep(1)
????print('Have a nice day???:(')
```
最終翻車
`2020/4/12 4:16:56 `
還未測(cè)試芯勘,今天會(huì)測(cè)試箱靴,個(gè)人感覺會(huì)翻車,建議不要使用荷愕,請(qǐng)持續(xù)關(guān)注衡怀,測(cè)試成功后可使用
`2020/4/12 11:00:19 `經(jīng)測(cè)試,無法獲取地理位置安疗,原因可能是我沒用儲(chǔ)存的cookie請(qǐng)求定位
`2020/4/12 12:34:26 `我搞不定請(qǐng)求定位的了抛杨,沒有達(dá)到預(yù)期效果,只有我自己能用荐类,其他人如果想用就要去抓一下獲取地址的請(qǐng)求怖现。我已經(jīng)加入了計(jì)劃任務(wù):
還得學(xué)好了再說啊,我把這破操作總結(jié)為:`萌新沒學(xué)走路就開跑`掉冶,但也是一個(gè)學(xué)習(xí)的過程了真竖。劃入歸檔啦脐雪,byebye!
原文地址:使用Python一鍵完成健康打卡