Selenium-Wire 开源项目教程
1. 项目的目录结构及介绍
Selenium-Wire 是一个用于拦截和检查浏览器发出的 HTTP 请求的工具。以下是其主要目录结构及其功能介绍:
selenium-wire/
├── docs/
│ ├── conf.py
│ ├── index.rst
│ └── ...
├── seleniumwire/
│ ├── backend.py
│ ├── certutils.py
│ ├── proxy/
│ │ ├── cert.py
│ │ ├── handler.py
│ │ └── ...
│ ├── request.py
│ ├── storage.py
│ └── ...
├── tests/
│ ├── test_backend.py
│ ├── test_request.py
│ └── ...
├── setup.py
├── README.md
└── ...
docs/
: 包含项目文档的配置文件和文档源文件。seleniumwire/
: 核心代码目录,包含代理、请求处理、存储等相关模块。tests/
: 包含项目的测试代码。setup.py
: 用于安装项目的配置文件。README.md
: 项目介绍和使用说明。
2. 项目的启动文件介绍
Selenium-Wire 的启动文件主要是 seleniumwire/__init__.py
,它初始化了 Selenium-Wire 的核心功能,并提供了与 Selenium WebDriver 的集成。
from .webdriver import WebDriver
from .proxy.request import RequestInterceptor
from .proxy.storage import RequestStorage
from .proxy.handler import InterceptRequestHandler
from .proxy.cert import CertificateManager
from .backend import MitmProxy
from .options import Options
__version__ = '4.7.2'
def get_options(options=None):
return Options.default(options)
def create_proxy(options=None):
options = get_options(options)
cert_manager = CertificateManager(options)
storage = RequestStorage(options)
interceptor = RequestInterceptor(options, storage)
backend = MitmProxy(options, cert_manager, interceptor)
return backend
def WebDriver(*args, **kwargs):
return WebDriver(*args, **kwargs)
WebDriver
: 集成 Selenium WebDriver 的类。RequestInterceptor
: 请求拦截器。RequestStorage
: 请求存储器。InterceptRequestHandler
: 请求处理程序。CertificateManager
: 证书管理器。MitmProxy
: 中间人代理。Options
: 配置选项。
3. 项目的配置文件介绍
Selenium-Wire 的配置文件主要是 seleniumwire/options.py
,它定义了项目的配置选项。
class Options:
def __init__(self, options=None):
self.options = options or {}
@classmethod
def default(cls, options=None):
default_options = {
'addr': '127.0.0.1',
'port': 12345,
'proxy': {
'http': 'http://localhost:8080',
'https': 'https://localhost:8080',
'no_proxy': 'localhost,127.0.0.1'
},
'request_storage_base_dir': None,
'disable_encoding': False,
'enable_har': False,
'ignore_http_methods': [],
'max_body_size': 0,
'mitm_host': '127.0.0.1',
'mitm_port': 8080,
'ca_cert': None,
'ca_key': None,
'ca_passphrase': None,
'verify_ssl': True,
'suppress_connection_errors': True,
'backend': 'mitmproxy',
'request_storage': 'memory',
'scopes': [],
'headers': {},
'params': {},
'cookies': {},
'rewrite': [],
'ignore_http_methods': [],
'disable_capture': False,
'disable_encoding': False,
'enable_har': False,
'max_body_size': 0,
'mitm_host