python+request实现疫情上报

1.准备工作

在windows环境下使用python3.9版本进行开发,需要在cmd控制台使用pip install request指令安装request模块。

2.代码构思

去年用selenium自动化测试的思想实现了自动打卡,但因为服务器性能有限,而且如果页面元素加载变慢会导致selenium捕获不到dom元素,无法实现模拟鼠标操作的功能,有失败的可能性,想同时为多人打卡也只能用windows自动的定时功能错开实现,同时跑多个代码意味打开多个浏览器,服务器性能承受不住,效率比较低下。所以转变思路,用request来实现。直接发送请求给服务器来实现该功能。

2.1.摸清上报流程

先是进行登录,在chrome浏览器使用f12查看network确认客户端发送了哪些请求,这个代码应该是使用ssh进行编写的,所以需要过滤下请求,这里我过滤出所有以.do结尾的请求(struts方面的知识,可以自己去了解)
在这里插入图片描述
输入完账户密码点击登录后,我们可以发现浏览器请求了哪个地址,发送了什么参数,服务器响应了什么。可以查看服务器响应了cookie,名称为_WEU,但是有两个同名的,网上查阅了资料暂时没看到解决方法,不过代码获取到cookie是能够使用的。
在这里插入图片描述
代码比较简单,有几个注意点,一是请求方式是POST还是GET,二是requests.post(url=login_url,headers=headers,data=body)这里是data还是params取决于上图最后一个红框叫form data还是query string parameters。三是请求头需要包含哪些数据,有些请求可能会识别设备,自己看需求添加上。

import requests
import json
import configparser
import datetime
gloab_route = ''
def login():
    login_url = 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/emapfunauth/loginValidate.do'
    headers = {
        "Accept": "application/json, text/plain, */*"
    }
    body = {
        "userName": "123456",
        "password": "123456",
        "isWeekLogin": False
    }
    try:
        response = requests.post(url=login_url,headers=headers,data=body)
        cookies = response.cookies
        _WEU = cookies['_WEU']
        cookie = '_WEU={0}'.format(_WEU)
        print(cookie)
        if cookie is not None:
            print(response.content)
            print('获取登录cookies成功')
        else:
            print('获取登录cookies失败')
        return cookie
    except Exception as error:
        print('获取登录cookies失败')

原本以为登陆后拿到cookie就结束了,但发现这个cookie并不是我填写今日身体状况所需要的那个cookie,也就是当你拿到这个cookie后,浏览器还利用这个cookie去请求其他cookie,一个套娃操作,所以我实现了一次完整手动数据上报后,整理出他最终的流程。
下面这张图不难看出,request header携带的cookie就是刚刚从loginValidate.do里面获取的cookie,route也是,和_WEU获取是一样,写这篇文章前请求redirect的cookie并不需要route,但现在却需要了,实际根据获取情况调试代码=。
在这里插入图片描述
这个代码就是get请求,参数类型是params,而且他获取了route。

def get_redirect_cookie():
    redirect_url = 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/emaphome/redirect.do'
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Cookie":login(),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    params = {
        "service":"/xsdtfw/sys/swmxsyqxxsjapp/*default/index.do?wxType=1"
    }
    try:
        response = requests.get(url=redirect_url,headers=headers,params=params,allow_redirects=False)
        cookies = response.cookies
        route = cookies['route']
        _WEU = cookies['_WEU']
        cookie = 'route={0};_WEU={1}'.format(
            route,_WEU
        )
        if cookie is not None:
            # print('这是redirect的cookie')
            # print(cookie)
            print('获取redirect cookies成功')
        else:
            print('获取redirect cookies失败')
        return cookie
    except Exception as error:
        print('获取redirect cookies失败')

接下来是重复型操作,目的是为了找到saveStuxx.do这个上报今日身体状况的cookie到底来自哪里,然后顺着上个.do去找,最终可以发现最开始的cookie来自登录那里,并模拟form data填写自己的信息即可。
在这里插入图片描述
下面是剩下的代码

def get_app_cookie():
    app_url = 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/emaphome/appShow.do'
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Cookie":get_redirect_cookie(),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    params = {
        "name":"swmxsyqxxsjapp"
    }
    try:
        response = requests.get(url=app_url,headers=headers,params=params,allow_redirects=False)
        cookies = response.cookies
        _WEU = cookies['_WEU']
        cookie = '_WEU={0};EMAP_LANG=zh;THEME=purple'.format(_WEU)
        if cookie is not None:
            # print('这是app的cookie')
            # print(cookie)
            print('获取app cookies成功')
        else:
            print('获取app cookies失败')
        return cookie
    except Exception as error:
        print('获取app cookies失败')
def get_role_cookie():
    role_url = 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/swpubapp/MobileCommon/getSelRoleConfig.do'
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Cookie": get_app_cookie(),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    body = {
        "APPID":"5811260348942403",
        "APPNAME":"swmxsyqxxsjapp"
    }
    try:
        response = requests.post(url=role_url,headers=headers,data=body)
        cookies = response.cookies
        _WEU = cookies['_WEU']
        route = cookies['route']
        global gloab_route
        gloab_route = route
        # print(route+' 来自role')
        cookie = '_WEU={0}; EMAP_LANG=zh; THEME=purple;route={1}'.format(_WEU,route)
        if cookie is not None:
            # print('这是role的cookie')
            # print(cookie)
            print('获取role cookies成功')   
        else:
            print('获取role cookies失败')
        return cookie
    except Exception as error:
        print('获取role cookies失败')
def get_today_cookie():
    role_url = 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/emappagelog/config/swmxsyqxxsjapp.do'
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Cookie": get_role_cookie(),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    try:
        response = requests.post(url=role_url,headers=headers)
        cookies = response.cookies
        _WEU = cookies['_WEU']
        # print(gloab_route)
        cookie = '_ht=person; EMAP_LANG=zh; THEME=purple; _WEU={0}; route={1}'.format(_WEU,gloab_route)
        # print(cookie)
        if cookie is not None:
            print('获取today cookies成功')
        else:
            print('获取today cookies失败')
        return cookie
    except Exception as error:
        print('获取today cookies失败')
def modify_temp():
    commit_temp_url = "http://xgfx.bnuz.edu.cn/xsdtfw/sys/swmxsyqxxsjapp/modules/mrbpa/saveStuXx.do"
    cookie = get_today_cookie()
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Cookie":cookie,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Content-Type":"application/x-www-form-urlencoded",
        "Accept-Language":"zh-CN,zh;q=0.9"
    }
    # print(cookie)
    date = datetime.datetime.now().strftime("%Y-%m-%d")
    data = {"ZYDM":"0103","GJDQ":"156","SFDFHB_DISPLAY":"否","SFJCQZ":"0"}
    body = {
        "data":json.dumps(data)
    }
    # print(body)
    try:
        res = requests.post(url=commit_temp_url,headers=headers,data=body)
        #print(res.status_code)
        #print(res.encoding)
        print(res.content)
        print('申报成功')
    except Exception as error:
        print('申报失败')
# modify_temp()

3.总结

实际上添加是否成功是需要自己查看的,出于隐私原因我就不分享了,这个代码也只是用来交流下思路罢了,有问题可以在评论区交流

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值