背景:因为某些原因,每天上网必须登录某个网页,觉得很麻烦 所以就写了这样一个自动登录网页的东西
使用软件Fiddler.exe
注:无法识别https时,可安装Fiddler https证书,具体怎样安装 百度一下你就知道
- 在网页填写好账号密码,准备登陆
- 打开Fiddler软件, 设置好监控的浏览器
更新: 2020年5月,突然发现抓不到浏览器请求, 360急速的抓不到了,可以换成IE或edge不进行指定进程,即可抓到
- 在准备好的网页上点击登陆
- 在Fiddler中查看截获的信息
- 如图所示,查看此类型请求,点击软件右侧web图表选项,进行查看
- 找到带有账号密码的数据连接 ,按照下面的代码,将之对照填写
- 将下面代码中的URL替换为当前数据的URL
Python代码:
import urllib.request
import urllib
import gzip
import http.cookiejar
import sys
import os
# 定义一个方法用于生成请求头信息,处理cookie
def getOpener(head):
# deal with the Cookies
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 ungzip(data):
try: # 尝试解压
print('正在解压.....')
data = gzip.decompress(data)
print('解压完毕!')
except:
print('未经压缩, 无需解压')
return data
# 封装头信息,伪装成浏览器,将连接标头内的键值对复制于此
header = {
'Connection': 'keep-alive',
'Host': '20.20.20.2',
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
# 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
url = 'http://20.20.20.2/ac_portal/login.php'#右键点击选中连接,将复制的URL粘贴于此
opener = getOpener(header)
id = 'admin' # 你的用户名
password = '*******' # 你的密码,需要填写!!
postDict = {
'opr': 'pwdLogin',
'userName': id,
'pwd': password,
'rememberPwd': '0',
}
postData = urllib.parse.urlencode(postDict).encode()
try:
op = opener.open(url, postData)
except:
print("登录失败")
os.system('pause') # 按任意键继续
sys.exit(0)
data = op.read()
data = ungzip(data)
print(data)
也可以用pyinstaller打包成exe文件 放其他电脑上都可以用
学习来源:https://blog.csdn.net/u283056051/article/details/49946981/