thewebscraping/tls-requests

GitHub: thewebscraping/tls-requests

提供基于浏览器指纹与 TLS 模拟的安全 HTTP 请求库,解决反机器人检测与抓包难题。

Stars: 150 | Forks: 10

# TLS 请求 [![GitHub License](https://img.shields.io/github/license/thewebscraping/tls-requests)](https://github.com/thewebscraping/tls-requests/blob/main/LICENSE) [![CI](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/4d607db46d181641.svg)](https://github.com/thewebscraping/tls-requests/actions/workflows/ci.yml) [![PyPI - Version](https://img.shields.io/pypi/v/wrapper-tls-requests)](https://pypi.org/project/wrapper-tls-requests/) [![Sponsor](https://img.shields.io/badge/Sponsor-thewebscraping-pink?logo=github-sponsors&logoColor=white)](https://github.com/sponsors/thewebscraping) ![Python Version](https://img.shields.io/badge/Python-3.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-blue?style=flat) ![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white) [![](https://img.shields.io/badge/Pytest-Linux%20%7C%20MacOS%20%7C%20Windows-blue?style=flat&logo=pytest&logoColor=white)](https://github.com/thewebscraping/tls-requests) [![Documentation](https://img.shields.io/badge/Mkdocs-Documentation-blue?style=flat&logo=MaterialForMkDocs&logoColor=white)](https://thewebscraping.github.io/tls-requests/) TLS Requests 是一个功能强大的 Python 库,用于安全的 HTTP 请求,提供类似浏览器的 TLS 客户端、指纹识别、反机器人页面绕过以及高性能。 ## **安装** 要安装该库,您可以选择以下两种方法之一: #### **1. 通过 PyPI 安装:** ``` pip install wrapper-tls-requests ``` #### **2. 通过 GitHub 仓库安装:** ``` pip install git+https://github.com/thewebscraping/tls-requests.git ``` ## **快速开始** 只需几行代码即可开始使用 TLS Requests。它会根据所选的浏览器标识符自动同步头部信息: ``` import tls_requests # 该库自动注入匹配的 User-Agent 和 Sec-CH-UA 标头 r = tls_requests.get("https://httpbin.org/headers", client_identifier="chrome_133") r.json()["headers"]["User-Agent"] 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36' ``` 基本配置会自动为代理和 TLS 标识符轮换: ``` import tls_requests proxy_rotator = tls_requests.ProxyRotator([ "http://user1:pass1@proxy.example.com:8080", "http://user2:pass2@proxy.example.com:8081", "socks5://proxy.example.com:8082", "proxy.example.com:8083", # defaults to http "http://user:pass@proxy.example.com:8084|1.0|US", # weight and region support ]) r = tls_requests.get( "https://httpbin.org/get", proxy=proxy_rotator, client_identifier=tls_requests.TLSIdentifierRotator() ) r r.status_code 200 ``` ## **介绍** **TLS Requests** 是一个面向 Python 的先进 HTTP 客户端,提供功能丰富、 高度可配置的替代方案,用于流行的 [`requests`](https://github.com/psf/requests) 库。 基于 [`tls-client`](https://github.com/bogdanfinn/tls-client) 构建, 它在易用性与高级功能之间取得平衡,适用于安全网络通信。 ### **主要优势** * **绕过 TLS 指纹识别**:模拟类似浏览器的行为以绕过复杂的反机器人系统。 * **可定制的 TLS 客户端**:选择特定的 TLS 指纹以满足需求。 * **适合开发者**:轻松构建网络爬虫、API 客户端或其他自定义网络工具。 ## **为何使用 TLS Requests?** 现代网站越来越多地使用 **TLS 指纹识别** 和类似 Cloudflare Bot Fight Mode 的反机器人工具来阻止网络爬虫。 **TLS Requests** 通过模拟类似浏览器的 TLS 行为来绕过这些障碍, 从而轻松抓取数据或与使用复杂反机器人措施的网站进行交互。 ### **解锁 Cloudflare Bot Fight Mode** ![coingecko.png](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/dce99a9717181643.png) **示例代码:** ``` import tls_requests r = tls_requests.get('https://www.coingecko.com/') r ``` ## **主要特性** ### **增强能力** * **类似浏览器的 TLS 指纹识别**:实现安全可靠的浏览器模拟连接。 * **动态头部同步**:自动从 `client_identifier` 提取浏览器版本并注入到 `User-Agent` 和 `sec-ch-ua` 头部。 * **高性能后端**:基于 Go 实现的 HTTP 后端,默认启用 **Protocol Racing**(快乐眼球算法)以实现更快的连接。 * **同步与异步支持**:在同步与异步请求之间无缝切换。 * **协议支持**:完全兼容 HTTP/1.1、HTTP/2 和 HTTP/3(Alpha)。 * **严格的超时控制**:可靠的超时管理,精确控制请求持续时间。 ### **其他特性** * **国际化域名与 URL 支持**:轻松处理非 ASCII URL。 * **Cookie 管理**:确保基于会话的 Cookie 持久化。 * **身份验证**:原生支持 Basic 和 Function 身份验证。 * **内容解码**:自动处理 gzip 和 brotli 编码的响应。 * **钩子机制**:适用于日志记录、监控、跟踪或请求/响应前后处理。 * **Unicode 支持**:轻松处理 Unicode 响应体。 * **高级 TLS 选项**:支持 `protocol_racing`、`allow_http` 和 `stream_id`。 * **文件上传**:简化的多部分文件上传支持。 * **代理配置**:支持 Socks5、HTTP 和 HTTPS 代理以增强隐私。 ## **文档** 在文档中探索 TLS Requests 的全部功能: * **[快速入门指南](https://thewebscraping.github.io/tls-requests/quickstart/)**:面向初学者的指南。 * **[高级主题](https://thewebscraping.github.io/tls-requests/advanced/client/)**:学习使用高级特性。 * **[异步支持](https://thewebscraping.github.io/tls-requests/advanced/async_client/)**:处理高并发场景。 * **自定义 TLS 配置**: * **[Wrapper TLS 客户端](https://thewebscraping.github.io/tls-requests/tls/)** * **[TLS 客户端配置文件](https://thewebscraping.github.io/tls-requests/tls/profiles/)** * **[自定义 TLS 配置](https://thewebscraping.github.io/tls-requests/tls/configuration/)** 阅读文档:[**thewebscraping.github.io/tls-requests/**](https://thewebscraping.github.io/tls-requests/) ## **报告问题** 发现错误?请[打开问题](https://github.com/thewebscraping/tls-requests/issues/)。 通过报告问题,您可以帮助改进项目。 ## **致谢** 特别感谢 [bogdanfinn](https://github.com/bogdanfinn/) 创建了出色的 [tls-client](https://github.com/bogdanfinn/tls-client)。
标签:BeEF, HTTPS, HTTP请求, Linux取证, Mkdocs, pre-commit, PyPI, Pytest, Python3.10, Python3.11, Python3.12, Python3.9, SSL/TLS客户端, TLS, 反机器人绕过, 安全通信, 开源库, 性能优化, 搜索引擎爬虫, 文档, 日志审计, 检测绕过, 浏览器指纹, 爬虫, 网络请求, 赞助, 逆向工具, 防御工具