Add multi-browser support and HTTP fallback for cookie acquisition

This commit is contained in:
2026-03-02 21:48:13 +08:00
parent 0f3dd8f287
commit 74d40cea4f
2 changed files with 249 additions and 387 deletions

View File

@@ -1,11 +1,13 @@
import os
import time
import requests
COOKIE_FILE = r"C:\Users\Administrator\Desktop\TikTokDownload-main\douyin_cookie.txt"
def get_douyin_cookie_auto(force_refresh=False, headless=True):
"""
自动获取抖音访客Cookie使用Edge本地驱动
自动获取抖音访客Cookie
支持多种方式Edge、Chrome、Firefox、纯HTTP请求
"""
# 检查是否已有Cookie且不需要强制刷新
@@ -21,58 +23,84 @@ def get_douyin_cookie_auto(force_refresh=False, headless=True):
print("[自动Cookie] 开始自动获取...")
# 方法1: 尝试使用Selenium支持Edge/Chrome/Firefox
cookie = try_selenium_cookie(headless)
if cookie:
return cookie
# 方法2: 纯HTTP请求方式不依赖浏览器
print("[自动Cookie] Selenium不可用尝试HTTP请求方式...")
cookie = try_http_cookie()
if cookie:
return cookie
print("[自动Cookie] 错误: 所有自动获取方式都失败")
print("[自动Cookie] 提示: 请手动获取Cookie")
return None
def try_selenium_cookie(headless=True):
"""尝试使用Selenium获取Cookie支持Edge/Chrome/Firefox"""
try:
# 尝试使用selenium with本地Edge
from selenium import webdriver
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.edge.service import Service as EdgeService
print("[自动Cookie] 启动Edge浏览器...")
driver = None
options = EdgeOptions()
if headless:
print("[自动Cookie] 使用无头模式...")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
# 尝试使用系统Edge驱动
# 尝试Edge
try:
# 方法1: 直接使用Edge自动查找驱动
from selenium.webdriver.edge.options import Options as EdgeOptions
print("[自动Cookie] 尝试启动Edge浏览器...")
options = EdgeOptions()
if headless:
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Edge(options=options)
print("[自动Cookie] Edge浏览器启动成功(自动驱动)")
except Exception as e1:
print(f"[自动Cookie] 自动驱动失败: {e1}")
print("[自动Cookie] Edge浏览器启动成功")
# 方法2: 尝试常见驱动路径
common_paths = [
r"C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe",
r"C:\Program Files\Microsoft\Edge\Application\msedgedriver.exe",
os.path.expandvars(r"%PROGRAMFILES%\Microsoft\Edge\Application\msedgedriver.exe"),
os.path.expandvars(r"%PROGRAMFILES(X86)%\Microsoft\Edge\Application\msedgedriver.exe"),
]
except Exception as e:
print(f"[自动Cookie] Edge失败: {str(e)[:50]}")
driver = None
for driver_path in common_paths:
if os.path.exists(driver_path):
print(f"[自动Cookie] 尝试使用驱动: {driver_path}")
try:
service = EdgeService(executable_path=driver_path)
driver = webdriver.Edge(service=service, options=options)
print("[自动Cookie] Edge浏览器启动成功指定驱动")
break
except Exception as e2:
print(f"[自动Cookie] 驱动 {driver_path} 失败: {e2}")
continue
if driver is None:
raise Exception("无法找到可用的Edge驱动")
# 尝试Chrome
try:
from selenium.webdriver.chrome.options import Options as ChromeOptions
print("[自动Cookie] 尝试启动Chrome浏览器...")
options = ChromeOptions()
if headless:
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
print("[自动Cookie] Chrome浏览器启动成功")
except Exception as e2:
print(f"[自动Cookie] Chrome失败: {str(e2)[:50]}")
# 尝试Firefox
try:
from selenium.webdriver.firefox.options import Options as FirefoxOptions
print("[自动Cookie] 尝试启动Firefox浏览器...")
options = FirefoxOptions()
if headless:
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
print("[自动Cookie] Firefox浏览器启动成功")
except Exception as e3:
print(f"[自动Cookie] Firefox失败: {str(e3)[:50]}")
if driver is None:
return None
# 访问抖音
print("[自动Cookie] 访问抖音网站...")
@@ -93,7 +121,6 @@ def get_douyin_cookie_auto(force_refresh=False, headless=True):
cookie_str = "; ".join([f"{c['name']}={c['value']}" for c in cookies])
print(f"[自动Cookie] 成功获取 {len(cookies)} 个Cookie项")
print(f"[自动Cookie] Cookie项: {', '.join([c['name'] for c in cookies])}")
print(f"[自动Cookie] Cookie长度: {len(cookie_str)}")
# 保存
@@ -107,25 +134,90 @@ def get_douyin_cookie_auto(force_refresh=False, headless=True):
driver.quit()
return cookie_str
except ImportError as e:
print(f"[自动Cookie] 错误: selenium未安装 - {e}")
print("[自动Cookie] 提示: 请运行 pip install selenium")
except ImportError:
print("[自动Cookie] Selenium未安装,跳过浏览器方式")
return None
except Exception as e:
print(f"[自动Cookie] 错误: {e}")
import traceback
traceback.print_exc()
print(f"[自动Cookie] Selenium方式失败: {e}")
return None
def try_http_cookie():
"""使用纯HTTP请求获取Cookie不依赖浏览器"""
try:
print("[自动Cookie] 使用HTTP请求方式...")
session = requests.Session()
# 模拟正常浏览器访问
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
}
session.headers.update(headers)
# 访问抖音首页
print("[自动Cookie] 访问抖音首页...")
resp = session.get("https://www.douyin.com", timeout=15)
print(f"[自动Cookie] 响应状态: {resp.status_code}")
# 提取Cookie
cookies = session.cookies.get_dict()
if cookies:
# 转换为字符串
cookie_str = "; ".join([f"{name}={value}" for name, value in cookies.items()])
print(f"[自动Cookie] 成功获取 {len(cookies)} 个Cookie项")
print(f"[自动Cookie] Cookie项: {', '.join(cookies.keys())}")
print(f"[自动Cookie] Cookie长度: {len(cookie_str)}")
# 保存
try:
with open(COOKIE_FILE, 'w', encoding='utf-8') as f:
f.write(cookie_str)
print(f"[自动Cookie] Cookie已保存")
except Exception as e:
print(f"[自动Cookie] 保存失败: {e}")
return cookie_str
else:
print("[自动Cookie] HTTP方式未获取到Cookie")
return None
except requests.exceptions.Timeout:
print("[自动Cookie] HTTP请求超时")
return None
except requests.exceptions.ConnectionError:
print("[自动Cookie] 网络连接失败")
return None
except Exception as e:
print(f"[自动Cookie] HTTP方式失败: {e}")
return None
if __name__ == "__main__":
print("="*60)
print("测试自动获取CookieEdge本地驱动")
print("测试自动获取Cookie多浏览器+HTTP方式")
print("="*60)
cookie = get_douyin_cookie_auto(force_refresh=True, headless=False)
cookie = get_douyin_cookie_auto(force_refresh=True, headless=True)
if cookie:
print(f"\n成功Cookie长度: {len(cookie)}")
print(f"Cookie前200字符:\n{cookie[:200]}...")
else:
print("\n失败无法获取Cookie")
print("\n失败:无法自动获取Cookie")
print("\n请手动获取Cookie")
print("1. 打开浏览器访问 www.douyin.com")
print("2. 按F12打开开发者工具")
print("3. 刷新页面点击Network标签")
print("4. 点击任意请求复制Cookie值")