224 lines
8.1 KiB
Python
224 lines
8.1 KiB
Python
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、Chrome、Firefox、纯HTTP请求
|
||
"""
|
||
|
||
# 检查是否已有Cookie且不需要强制刷新
|
||
if not force_refresh and os.path.exists(COOKIE_FILE):
|
||
try:
|
||
with open(COOKIE_FILE, 'r', encoding='utf-8') as f:
|
||
cookie = f.read().strip()
|
||
if cookie and len(cookie) > 100:
|
||
print("[自动Cookie] 使用已保存的Cookie")
|
||
return cookie
|
||
except Exception as e:
|
||
print(f"[自动Cookie] 读取Cookie失败: {e}")
|
||
|
||
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:
|
||
from selenium import webdriver
|
||
|
||
driver = None
|
||
|
||
# 尝试Edge
|
||
try:
|
||
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 e:
|
||
print(f"[自动Cookie] Edge失败: {str(e)[:50]}")
|
||
|
||
# 尝试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] 访问抖音网站...")
|
||
driver.get("https://www.douyin.com")
|
||
|
||
print("[自动Cookie] 等待Cookie生成(5秒)...")
|
||
time.sleep(5)
|
||
|
||
# 提取Cookie
|
||
cookies = driver.get_cookies()
|
||
|
||
if not cookies:
|
||
print("[自动Cookie] 警告: 未获取到Cookie")
|
||
driver.quit()
|
||
return None
|
||
|
||
# 转换为字符串
|
||
cookie_str = "; ".join([f"{c['name']}={c['value']}" for c in cookies])
|
||
|
||
print(f"[自动Cookie] 成功获取 {len(cookies)} 个Cookie项")
|
||
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}")
|
||
|
||
driver.quit()
|
||
return cookie_str
|
||
|
||
except ImportError:
|
||
print("[自动Cookie] Selenium未安装,跳过浏览器方式")
|
||
return None
|
||
except Exception as e:
|
||
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("测试:自动获取Cookie(多浏览器+HTTP方式)")
|
||
print("="*60)
|
||
|
||
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("1. 打开浏览器访问 www.douyin.com")
|
||
print("2. 按F12打开开发者工具")
|
||
print("3. 刷新页面,点击Network标签")
|
||
print("4. 点击任意请求,复制Cookie值")
|