admin管理员组

文章数量:1279176

I'm trying to login into / with cookies using curl_cffi in python, but it doesn't work.

I can manually use the cookies to login by using anti detect browsers like "Dolphin Anty" and cookie editor extension.

How can I achieve this in python using requests or curl_cffi without using selenium or playwright?

My code:

from curl_cffi import requests
import os

def checkCookie(cookie):
    cookie_path = os.path.join("input", cookie)
    with open(cookie_path, 'r', encoding='utf-8') as f:
        read_cookie = f.read()

        cookies = {}

        for line in read_cookie.splitlines():
            parts = line.strip().split('\t')
            if len(parts) >= 7:
                domain, _, path, secure, expires, name, value = parts[:7]
                cookies[name] = value

        session = requests.Session()
        session.cookies.update(cookies)
        
        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',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
            'Accept-Language': 'en-IN,en;q=0.9',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-User': '?1',
            'sec-ch-ua': '"Not A(Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'Referer': '/',
            'Origin': '',
        }
        
        response = session.get("/", headers=headers, impersonate="chrome", timeout=30, verify=True)

        if "youraccount_steamid" in response.text:
            print("Logged in")
        else:
            print("Failed")

        with open('test.html', 'w', encoding='utf-8') as f:
            f.write(response.text)

checkCookie("cookie.txt")

本文标签: pythonAuthenticating with cookies using requests or curlcffi in site doesn39t workStack Overflow