前言
因为公司业务问题导致我每天都得核对一下本天导入的数据,要是我自己一个一个对肯定是很耗费时间跟精力的,但是要是我写出一个脚本出来对要检查的数据做个筛选这样就简单多了。
准备工作
在自己电脑上安装JAVA、Python并配置环境变量(ps:这个我就不写了 偷个懒~)
导入需要使用的包(使用pip 或者直接在pycharm上安装就行 我使用的pip 直接安装的最新版的)
pip install requests
pip install selenium
pip install selenium-wire
注意 因为selenium的话需要使用webdiver驱动 所以一定要把驱动下载好并放到Python安装目录下配置好环境变量
Chrome驱动
火狐驱动
设计思路
通过 selenium-wire获取到cookie 然后使用 request获取数据并对比数据,当数据差额大于预期时打印出来
代码部分
首先是导入包
import time
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import requests
from datetime import datetime
获取cookie中需要的值
def getheader():
wd=webdriver.Chrome()
#最大化浏览器
wd.maximize_window()
#进入到登录页 url我就不方便填出来了毕竟是公司的嘛
wd.get(url)
time.sleep(3)
#由于我们的前端使用了iframe所以需要 switch_to 然后进入到这个子节点
wd.switch_to.frame(wd.find_element(By.XPATH,"//*[@frameborder='0']"))
time.sleep(3)
#这里是登录 因为公司有个类似钉钉的客户端登录所以这里只要点击下就好了不需要再使用账号密码了 不得不说这个是真的方便
wd.find_element(By.XPATH,"//img[@class='qrcode_content___tdo_h']").click()
time.sleep(10)
#这里是父列表的点击
wd.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div[2]/div[1]/div/ul/div[4]/li/div/span').click()
time.sleep(2)
#子列表的点击
wd.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div[2]/div[1]/div/ul/div[4]/li/ul/div[1]/a/li/span').click()
time.sleep(2)
#此时 子列表的查询接口已经在 seleniumwire的请求列表中了 只需找到这个请求获取到想要的header的值就好了
for i in wd.requests:
# 可以通过下面打印看下 请求及请求的信息
# print(i, i.headers, i.response)
#通过判断这请求的地址是否在请求中来获取到这个查询列表的请求
if i.path=='/api/accounting/collection/list':
# 养成关闭驱动的习惯 虽然我经常不关hhh
wd.close()
wd.quit()
#获取header上的所需的参数
return i.headers['token'], i.headers['cookie']
数据比对及筛选
def contrast(token,cookie,collectionDate):
header={
'Accept':'application/json, text/plain, */*',
'Accept-Encoding':'Accept-Encoding',
'Content-Type':'application/json;charset=utf-8',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0',
'token':token,
'cookie':cookie
}
print(f"当前日期 {collectionDate}")
# 因为是个get请求 我这里直接使用字符串 将日期写url了
url=f'url?pageNum=1&pageSize=100&collectionDate={collectionDate}'
getdata=requests.get(url=url,headers=header)
print(getdata.json()['msg'])
# 下面就简单了 将不符合预期的数据跟batchId 打印出来
for i in getdata.json()['rows']:
nsdr = i['numberSourceDataRows'] * 0.08
if i['numberSourceDataRows']==i['collectedSuccessDataRows']:
continue
elif i['numberSourceDataRows']<i['collectedSuccessDataRows']:
continue
elif i['numberSourceDataRows']-i['collectedSuccessDataRows']<=nsdr:
print(f"{i['batchId']}--源数据:{i['numberSourceDataRows']} 成功:{i['collectedSuccessDataRows']} 存在差额但是小于8%")
else:
print(f"{i['batchId']}--源数据:{i['numberSourceDataRows']} 成功:{i['collectedSuccessDataRows']} 差额大于8% 请联系开发检查")
到了最后一步了调用函数执行
if __name__ == '__main__':
# 获取token跟cookie
token,cookie = getheader()
# 获取当前日期 年月日
collectionDate = datetime.today().date()
#对比打印
contrast(token, cookie, collectionDate)
好了到这里就大功告成了。我们看下执行的结果。哎嘿~有差额!开发大哥我来了!
总结
总的来说,使用脚本可以大大的节省我们的时间,也可以让我们测试磨练下自己的代码功底(大家看我代码就可以看出来我是个小菜鸡了吧 如果有什么更好的方案可以在评论区分享下哈 感谢啦)。
编写脚本过程中参考的文档:
帅胡-python之selenium wire获取请求头参数
安泽1314-python使用seleniumwire获取http请求头信息