使用Python一键完成健康打卡

2020-04-12  本文已影响0人  稚昂长

'xxx,你怎么没打卡,你是不是睡过头了?哈哈哈'

'你还没打卡,快去打卡啊,在线等,急!'

'.............'

来源:本人博客

萌生想法

  疫情以来每天都要完成健康打卡,说来惭愧,有时候我真的就忘了,还要去监督别人。在上次用Python给钓鱼网站提交垃圾信息后,我就想着用Python来实现一键提交,然后再配合windos的计划任务,每天自动提交岂不是美滋滋。

  今天` 2020年4月11日 `(过了0点),我完成了适合自己的脚本。不出意外的话下一个零点我就可以一键上报了。

看看实验图:

这里显示`今天已经填报过了`是因为我主动提交了信息,目的是方便我抓数据。

一天只能提交一次实在不方便我抓数据,其实昨天有机会来着,被我浪费了。


发现问题

  但是有问题存在

 - 别人怎么用?

 - 怎么登陆?

这个时候就要提到`Cookie`了

???什么饼干

Cookie

简单地说,某网站能`'一周免登陆'`就是用到了Cookie

百度百科:

Cookie,有时也用其复数形式 Cookies。类型为“小型文本文件”,是某些网站为了辨别用户身份,进行Session跟踪而储存在用户本地终端上的数据(通常经过加密),`由用户客户端计算机暂时或永久保存的信息`。

我的想法是:要想大家都能用还需要登录抓取Cookie

`现在2020/4/11 1:35:06 睡觉啦,明天继续`


终于成功

现在`2020/4/12 3:26:23 `我们继续

经过辣么辣么久的尝试(刚入门Python),我终于成功啦

代码我会放在最后,因为现在水平不高,大佬勿喷啊

`记得安装result库:pip install result`

 - `https://app.nwafu.edu.cn/uc/wap/login/check`模拟登录,保存cookie

 - `https://app.nwafu.edu.cn/ncov/wap/default/save`再继续请求健康上报页面

健康上报页面的要提交的header包含很多信息,在浏览器抓到的数据中

 - `https://webapi.amap.com/maps/ipLocation`是获取经纬度的的请求地址

 - `https://restapi.amap.com/v3/geocode/regeo`是获取详细地址的请求地址

提交详细地址请求后会返回

```json

jsonp_703113_({"status":"1","regeocode":{"addressComponent":{"city":"某某地区","province":"新省","adcode":"行政区号","district":"县","towncode":"行政区号","streetNumber":{"number":"街道号","location":"经度,纬度","direction":"方向","distance":"34.3624","street":"街道"},"country":"国家","township":"镇","businessAreas":[[]],"building":{"name":[],"type":[]},"neighborhood":{"name":[],"type":[]},"citycode":"区号"},"formatted_address":"详细地址"},"info":"OK","infocode":"10000"})

```

在这里我将这串数据赋值给`city0`,然后用字符串切片截取数据

```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行改好自己的学号密码,然后将这个脚本添加至计划任务就可以每天`自动打卡`了

不知道如何设置的可以参考:windows创建定时任务执行python脚本")

全部代码

```python

# !/usr/bin/env python

# -*- coding: utf-8 -*-

#请在12行补全账号密码

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'  # 登录教务系统的URL,目的是获取cookie

get_url = 'https://app.nwafu.edu.cn/ncov/wap/default/save'  # 利用cookie请求打卡地址

values = {'username': '在这里输入学号', '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('经度:',jdd,end='  ')

wdd=ipdz[ipdz.index('"lng":"')+7:ipdz.index('","lat"')]

print('纬度:',wdd)

#以上获取经纬度

############################################################################################################

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("行政区划代码:",adcode)

print('区号:',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已经写.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("欧耶!上报完成")

    print('完成,程序3秒后退出')

    print('3')

    time.sleep(1)

    print('2')

    time.sleep(1)

    print('1')

    time.sleep(1)

    print('Bye bye  :)')

elif '已经填报' in final:

    print("今天已经填报过啦   )QWQ )")

    print('完成,程序3秒后退出')

    print('3')

    time.sleep(1)

    print('2')

    time.sleep(1)

    print('1')

    time.sleep(1)

    print('Be seeing you  :)')

elif '请输入' in final:

    print('发生错误,请检查账号密码,程序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 `

还未测试,今天会测试,个人感觉会翻车,建议不要使用,请持续关注,测试成功后可使用

`2020/4/12 11:00:19 `经测试,无法获取地理位置,原因可能是我没用储存的cookie请求定位

`2020/4/12 12:34:26 `我搞不定请求定位的了,没有达到预期效果,只有我自己能用,其他人如果想用就要去抓一下获取地址的请求。我已经加入了计划任务:

还得学好了再说啊,我把这破操作总结为:`萌新没学走路就开跑`,但也是一个学习的过程了。划入归档啦,byebye!

原文地址:使用Python一键完成健康打卡

上一篇 下一篇

猜你喜欢

热点阅读