python爬虫- requests、httpx、aiohttp、scrapy突破ja3指纹识别
requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.ssl_ import create_urllib3_context import requests import random ORIGIN_CIPHERS = ( 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:' 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES' ) class DESAdapter(HTTPAdapter): def __init__( self , * args, * * kwargs): """ A TransportAdapter that re-enables 3DES support in Requests. """ CIPHERS = ORIGIN_CIPHERS.split( ':' ) random.shuffle(CIPHERS) CIPHERS = ':' .join(CIPHERS) self .CIPHERS = CIPHERS + ':!aNULL:!eNULL:!MD5' super ().__init__( * args, * * kwargs) def init_poolmanager( self , * args, * * kwargs): context = create_urllib3_context(ciphers = self .CIPHERS) kwargs[ 'ssl_context' ] = context return super (DESAdapter, self ).init_poolmanager( * args, * * kwargs) def proxy_manager_for( self , * args, * * kwargs): context = create_urllib3_context(ciphers = self .CIPHERS) kwargs[ 'ssl_context' ] = context return super (DESAdapter, self ).proxy_manager_for( * args, * * kwargs) headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67' } s = requests.Session() s.headers.update(headers) for _ in range ( 5 ): s.mount( 'https://ja3er.com' , DESAdapter()) resp = s.get( 'https://ja3er.com/json' ).json() print (resp) |
来源:点我
aiohttp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import random import ssl import asyncio import aiohttp # ssl._create_default_https_context = ssl._create_unverified_context ORIGIN_CIPHERS = ( 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:' 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES' ) class SSLFactory: def __init__( self ): self .ciphers = ORIGIN_CIPHERS.split( ":" ) def __call__( self ) - > ssl.SSLContext: random.shuffle( self .ciphers) ciphers = ":" .join( self .ciphers) ciphers = ciphers + ":!aNULL:!eNULL:!MD5" context = ssl.create_default_context() context.set_ciphers(ciphers) return context sslgen = SSLFactory() async def main(): async with aiohttp.ClientSession() as session: for _ in range ( 5 ): async with session.get( "https://ja3er.com/json" , headers = {}, ssl = sslgen()) as resp: data = await resp.json() print (data) asyncio.get_event_loop().run_until_complete(main()) # 专为win准备的写法 |
来源:点我,且这里面也有scrapy相关的ja3指纹修改
httpx
异步模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import httpx import asyncio import random import ssl ORIGIN_CIPHERS = ( 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:' 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES' ) class SSLFactory: def __init__( self ): self .ciphers = ORIGIN_CIPHERS.split( ":" ) def __call__( self ) - > ssl.SSLContext: random.shuffle( self .ciphers) ciphers = ":" .join( self .ciphers) ciphers = ciphers + ":!aNULL:!eNULL:!MD5" context = ssl.create_default_context() context.set_ciphers(ciphers) return context sslgen = SSLFactory() async def main(): async with httpx.AsyncClient(verify = sslgen()) as client: resp = await client.get( 'https://ja3er.com/json' ) result = resp.json() print (result) asyncio.run(main()) |
同步模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import httpx import asyncio import random import ssl ORIGIN_CIPHERS = ( 'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:' 'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES' ) class SSLFactory: def __init__( self ): self .ciphers = ORIGIN_CIPHERS.split( ":" ) def __call__( self ) - > ssl.SSLContext: random.shuffle( self .ciphers) ciphers = ":" .join( self .ciphers) ciphers = ciphers + ":!aNULL:!eNULL:!MD5" context = ssl.create_default_context() context.set_ciphers(ciphers) return context sslgen = SSLFactory() with httpx.Client(headers = {}, http2 = True , verify = sslgen()) as client: response = client.get( 'https://ja3er.com/json' ) print (response.text) |
分类:
python高级应用
, 逆向案例
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
2017-12-13 洗礼灵魂,修炼python(75)--全栈项目实战篇(3)—— 账户注册登录管理系统