代码来源青南大佬的文章:一日一技: Python 如何突破反爬虫指纹 JA3 - 墨天轮
原理是对python源码中ssl部分进行修改,对ORIGIN_CIPHERS中的加密方法进行打乱重组来修改TLS指纹
使用代理时需要注意的部分, 启用修改TLS设置s.mount("http://", DESAdapter()),第一个参数不能设置为 "https://", 否则会出现 AttributeError: 'NoneType' object has no attribute 'connection_from_url' 错误。
也有可能是我的代理不支持https的原因。
import random
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.ssl_ import create_urllib3_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 DESAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
CIPHERS = ORIGIN_CIPHERS.split(":")
random.shuffle(CIPHERS)
CIPHERS = ":".join(CIPHERS)
self.COPHERS = CIPHERS + ":!aNULL:!eNULL:!MD5"
super(DESAdapter, self).__init__(*args, **kwargs)
def init_poolmanager(self, *args, **kwargs):
context = create_urllib3_context(ciphers=self.COPHERS)
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.COPHERS)
kwargs["ssl_context"] = context
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0'
}
s = requests.Session()
s.headers.update(headers)
proxies = {
'http': 'xxx.xxx.xxx.xxx:9000',
'https': 'xxx.xxx.xxx.xxx:9000'
}
s.mount("http://", DESAdapter())
response = s.get('https://ja3er.com/json', proxies=proxies)
print(response.json())