安装mitmproxy

  • 安装:pip install mitmproxy
  • 验证:命令行 mitmproxy --version 不报错,安装成;
    安装成
  • mitmproxy 在用户目录下生成文件夹.mitproxy,其中包含各种证书。

浏览器设置代理

  • 打开Chrome,进行设置
    步骤一
  • 填入如下参数
    步骤二

编写代码

  • 架构,txt文件保存获得的cookie,html是利用cookie登录后获得的源码。
    架构
  • mitm.py,用于mitmproxy启动时自动保存cookie,代码如下:
# -*- coding: utf-8 -*-
"""
===========================
mitmproxy 过滤器:
    - 主要用于筛选包含指定域名网址的cookie,并将其保存至文件cookie.txt。
===========================
"""

# @Environment          : Windows10 + Python 3.8;
# @IDLE                 : PyCharm;
# @Install modules      : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx;
# @Project Name         : web;
# @File Name            : mitm.py;
# @Author Email         : wh98898@163.com;
# @Author Wechat_id     : i_biology;
# @Created Time         : 2022/4/11 0:37;

# TODO(i_biology) Created at 2022/4/11 0:37; 
#  Finished at 2022/4/11 0:37 

# built-in modules

# third modules

# user defined modules

# Constant

url = 'baidu.com'


def response(flow):
    # flow.request.headers["user-agent"] = "zhangwenhao"
    request = flow.request

    if '.png' in request.url or url not in request.url:
        return  # 如果不在观察的url内则返回
    if url in request.url:
        print(request.url)
        cookies = dict(request.cookies)  # 转换cookies格式为dict
        if cookies:
            save_cookies(repr(cookies))  # 如果不为空保存cookies


def save_cookies(cookies):
    with open("cookies.txt", "w") as f:
        f.write(cookies)
        f.flush()
  • login_baidu.py用于利用selenium登录百度,代码如下:
# -*- coding: utf-8 -*-
"""
===========================
登录百度
===========================
"""

# @Environment          : Windows10 + Python 3.8;
# @IDLE                 : PyCharm;
# @Install modules      : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx;
# @Project Name         : web;
# @File Name            : login_baidu.py;
# @Author Email         : wh98898@163.com;
# @Author Wechat_id     : i_biology;
# @Created Time         : 2022/4/11 0:47;

# TODO(i_biology) Created at 2022/4/11 0:47; 
#  Finished at 2022/4/11 0:47 

# built-in modules
import logging
from time import sleep

# third modules
from selenium import webdriver
from selenium.webdriver.common.by import By

# user defined modules

# Constant


logging.basicConfig(level=logging.INFO)  # 日志级别

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)

url = 'https://www.baidu.com/'
driver.get(url)  # 打开百度

driver.find_element(By.XPATH, '//*[@id="s-top-loginbtn"]').click()  # 点击登录
driver.find_element(By.XPATH, '//*[@id="TANGRAM__PSP_11__userName"]').send_keys("15097257986")  # 输入账号
driver.find_element(By.XPATH, '//*[@id="TANGRAM__PSP_11__password"]').send_keys("BD_zwh_910227!")  # 输入密码
driver.find_element(By.XPATH, '//*[@id="TANGRAM__PSP_11__submit"]').click()  # 点击登录

sec = 20  # 时间要长一些,因为需要手动拖动验证,注意控制手速
logging.info(f"休息{sec}秒后关闭浏览器")
sleep(sec)
driver.quit()
logging.info("浏览器已经关闭")

  • is_login_success.py验证cookie是否可以成功登录,代码如下:
# -*- coding: utf-8 -*-
"""
===========================
验证由mitmproxy获得的cookie是否可以成功登录百度
===========================
"""

# @Environment          : Windows10 + Python 3.8;
# @IDLE                 : PyCharm;
# @Install modules      : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx;
# @Project Name         : web;
# @File Name            : is_login_success.py;
# @Author Email         : wh98898@163.com;
# @Author Wechat_id     : i_biology;
# @Created Time         : 2022/4/11 0:45;

# TODO(i_biology) Created at 2022/4/11 0:45; 
#  Finished at 2022/4/11 0:45 

# built-in modules

# third modules
import requests

# user defined modules
import utils

# Constant
url = "https://www.baidu.com/my/index"
cookies = utils.get_cookie()
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/100.0.4896.75 Safari/537.36'}


re = requests.get(url=url, headers=header, cookies=cookies, verify=False, allow_redirects=True)
with open('baidu.html', 'w', encoding="utf-8") as f:
    print(re.content.decode("utf-8"), file=f)

  • utils.py工具,代码如下
# -*- coding: utf-8 -*-
"""
===========================
工具类
===========================
"""


# @Environment          : Windows10 + Python 3.8;
# @IDLE                 : PyCharm;
# @Install modules      : pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx;
# @Project Name         : web;
# @File Name            : utils.py;
# @Author Email         : wh98898@163.com;
# @Author Wechat_id     : i_biology;
# @Created Time         : 2022/4/11 0:40;

# TODO(i_biology) Created at 2022/4/11 0:40; 
#  Finished at 2022/4/11 0:40 

# built-in modules

# third modules

# user defined modules

# Constant

def get_cookie(cookie_file='cookies.txt'):
    with open(cookie_file) as f:
        return eval(f.read())

命令行运行

mitmweb -s mitm.py
python执行脚本login_baidu.py;
python执行脚本is_login_success.py;
本地浏览器打开baidu.html文件,截图如下,说明登录成功:
ok