python实现自动登录后台管理系统
作者:masserd 时间:2021-05-26 13:40:48
本文实例为大家分享了python实现自动登录后台管理系统的具体代码,供大家参考,具体内容如下
首先感谢下网络上的各位大神和博主,通过学习各位大神的文章,才实现了该脚本
①首先浏览器运行真是系统通过fiddler抓包,抓取到登录地址(后面的地址和头部信息等都是通过fiddler抓取的)
并获取头信息,header信息里面Accept-Encoding: gzip, deflate去掉吧,免得后面提取页面的url时无法解码,通过代码实现后,这个时候回返回html文本,从文本里面获取下一步要进入的系统的地址,这个地址已经附上cookie即token了,只有经过了这一步请求,才能进行下一步的对系统里面的内容进行操作(之前没有经过这一步,就进行了具体操作的url请求,总是返回重定位到登录界面)。
②然后再进行这个带token的url的请求
③请求完成后即可进行想要的操作了
下面直接上代码,有一些信息是公司的信息,我直接屏蔽换成通用字符串
# coding=utf-8
import urllib.request
import urllib
import http.cookiejar
from database_functions import *
import re
# 正则表达式匹配规则
regx = re.compile(r'(.*)(href=\")(.*)(\"\starget=.*bms.*)')
# 定义一个方法用于生成请求头信息,处理cookie
def getopener(head):
cj = http.cookiejar.CookieJar()
pro = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(pro)
header = []
for key, value in head.items():
elem = (key, value)
header.append(elem)
opener.addheaders = header
return opener
def modify_appeal_status_pass(phone):
index, status = get_appeal_by_phone(phone)
# 封装头信息,伪装成浏览器
header = {
'Connection': 'Keep-Alive',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Accept': 'application/json, text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'xxx.xxx.xxx',
}
# 登录地址
login_url = 'http://xxx.xxx.xxx/xx-xxx/login.do'
# request封装cookie和头信息
opener = getopener(header)
urllib.request.install_opener(opener)
username = '***' # 你的用户名
password = '***' # 你的密码
postdict = {
'username': username,
'password': password
}
# 登录请求
postdata = urllib.parse.urlencode(postdict).encode('utf-8')
login_response = urllib.request.Request(login_url, data=postdata, headers=header) #登录系统
login_webpage = urllib.request.urlopen(login_response)
# 返回的html页面
login_data = login_webpage.read().decode()
# 获取系统地址
bms_url = regx.findall(login_data)[0][2]
# 请求xxxx
bms_response = urllib.request.Request(bms_url, headers=header)
bms_webpage = urllib.request.urlopen(bms_response)
# 返回的系统后台页面
bms_data = bms_webpage.read().decode()
# 对xx状态进行修改
update_url = "http://xxx.xxx.xxx/xxx/xxx/xxx/xxx/{index}".format(index=index)
update_response = urllib.request.Request(update_url, headers=header)
update_webpage = urllib.request.urlopen(update_response)
update_data = update_webpage.read().decode()
print(update_data)
来源:https://blog.csdn.net/zoulonglong/article/details/79559166?utm_source=blogxgwz8
标签:python,自动登录,管理系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python爬虫进阶Scrapy框架精文讲解
2022-08-21 06:00:16
![](https://img.aspxhome.com/file/2023/3/95833_0s.jpg)
Python实现爬取知乎神回复简单爬虫代码分享
2023-03-23 19:00:10
![](https://img.aspxhome.com/file/2023/8/81778_0s.jpg)
Python统计分析模块statistics用法示例
2021-01-20 08:44:16
详解Go语言中关于包导入必学的 8 个知识点
2023-07-09 05:38:11
![](https://img.aspxhome.com/file/2023/9/87619_0s.png)
LRUCache的实现原理及利用python实现的方法
2022-06-26 06:51:51
python模块的安装以及安装失败的解决方法
2023-09-14 06:33:04
![](https://img.aspxhome.com/file/2023/8/62618_0s.png)
该用多大的字
2009-05-17 14:39:00
php控制文件下载速度的方法
2023-07-19 00:21:30
Python双精度浮点数运算并分行显示操作示例
2024-01-01 21:33:10
![](https://img.aspxhome.com/file/2023/4/105254_0s.jpg)
数据库理论:学习基于SQL数据库的算法
2009-01-13 13:38:00
浏览器发送URL的编码特性
2007-10-12 20:51:00
企业网站该怎么做?
2009-06-29 16:11:00
![](https://img.aspxhome.com/file/UploadPic/20096/29/how-to-make-the-company-website-step1-97s.jpg)
基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解
2023-04-09 17:28:45
![](https://img.aspxhome.com/file/2023/8/83958_0s.jpg)
Python基于百度AI实现OCR文字识别
2023-09-26 22:14:29
![](https://img.aspxhome.com/file/2023/3/90503_0s.png)
学习Django知识点分享
2021-07-18 03:42:00
支持python的分布式计算框架Ray详解
2023-06-20 11:34:30
![](https://img.aspxhome.com/file/2023/2/102912_0s.jpg)
asp 数组 重复删除函数代码
2011-03-03 10:47:00
MySQL两种表存储结构性能比较测试过程
2007-12-09 12:45:00
五分钟带你搞懂python 迭代器与生成器
2021-08-24 19:25:10
Python中super关键字用法实例分析
2023-12-08 06:11:46