过5s盾(cloudflare)的各种方案

2024-02-19  本文已影响0人  sexy_cyber
 pip install cloudscraper           
 pip install tls-client                
 pip install undetected-chromedriver   
pip3 install cfscrape
pip install curl_cffi

通过我构建的镜像,远程调用来获取cookies + headers,携带cooKies + headers发起get请求,即可

最好对容器定期重启,容器运行时间长 有效性会降低

# --port 86 暴露在容器外的端口号 --workers 20 并发数
docker run -d --restart always --network host --name cf-server uhub.service.ucloud.cn/drakespider/spider:cfserver0.29.0diy \
--host 0.0.0.0 --port 86 --workers 20
    def parse_proxy(self, proxy):
        """
        对代理格式化
        proxy: http://username:password@domain:port
        """
        if "@" in proxy:
            proxy_regex = re.match("(.+)://(.+):(.+)@(.+)", proxy)
            server = f"{proxy_regex.group(1)}://{proxy_regex.group(4)}"

            proxy_params = {
                "server": server,
                "username": proxy_regex.group(2),
                "password": proxy_regex.group(3),
            }
        else:
            proxy_params = {"server": proxy}

        return proxy_params
def get_rpc_result(self):
    resp = requests.post(
        "http://xxx:86", # rpc调用地址
        json={
            "proxy": browser_proxy,   # 支持代理方式
            "timeout": 30 * 60,      # 放弃的时间
            "url": "https://www.cloudflare.com" # 要绕过的网站
        },
        timeout=20 * 60
    )
res = self.get_rpc_result()
if res.get('success'):
    if res['cookies']:
        # {"__cf_bm": "u2nqgGBpHxg.jKrOidGVkbXqdUNor0B4VDts1dz1h4U-1708417453-1.0-AdywEwPEs3K4XFxncRtYrfP23b7nqF3ZM13fvhrgtSRYyA77aoFITGloNKgLNamkyEYc5tYaAeGH13+32YJ1a/Y=", "cf_clearance": "99EXeMZwYzCaJweUD1JxvVhSvBxqL0e60I4k_4BYJ1I-1708417456-1.0-AXnahzLUhbQgnO5LsIDomFDqRibU4icmN6/b2mLkrItzLCgEr0WpgccbOyeP2uJMWr1xuwI7MKiChMYhN/dXG2k="}
        cookies = res['cookies']
        # Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
        useragent = res['user_agent']

用容器生成UA、cookies、和发起rpc用的代理IP,去模拟请求

def mock_req(proxy, cookies, useragent, url):
    proxies = {
        'http': proxy,
        'https': proxy
    }
    headers = {
        "Referer": "https://www.cloudflare.com", # 目标网站的地址
        'User-Agent': useragent
    }
    # 发起get请求 url:目标接口的地址
    res = requests.get(url, headers=headers, proxies=proxies, cookies=cookies)

上一篇下一篇

猜你喜欢

热点阅读