下载地址:www.pan38.com/dow/share.p… 提取密码:2891
这个项目包含三个主要文件:主程序模块、入口文件和依赖文件。主程序实现了多平台Cookie提取功能,支持京东、淘宝、拼多多和抖音。使用时需要安装Chrome浏览器和对应驱动。
import os import json import time import platform from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager
class CookieExtractor: def init(self): self.driver = None self.cookies = {} self.platforms = { 'jd': { 'url': 'www.jd.com', 'login_url': 'passport.jd.com/new/login.a…', 'username_selector': '#loginname', 'password_selector': '#nloginpwd', 'submit_selector': '#loginsubmit' }, 'taobao': { 'url': 'www.taobao.com', 'login_url': 'login.taobao.com/member/logi…', 'username_selector': '#fm-login-id', 'password_selector': '#fm-login-password', 'submit_selector': '#login-form > div.fm-btn > button' }, 'pdd': { 'url': 'www.pinduoduo.com', 'login_url': 'mobile.yangkeduo.com/login.html', 'username_selector': '#user-mobile', 'password_selector': '#password-mobile', 'submit_selector': '#submit-mobile' }, 'douyin': { 'url': 'www.douyin.com', 'login_url': 'www.douyin.com/login', 'username_selector': 'input[name="username"]', 'password_selector': 'input[name="password"]', 'submit_selector': 'button[type="submit"]' } }
def init_driver(self):
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--disable-infobars")
if platform.system() == 'Linux':
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
self.driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=chrome_options
)
self.driver.maximize_window()
def login_platform(self, platform_name, username, password):
if platform_name not in self.platforms:
raise ValueError(f"Unsupported platform: {platform_name}")
platform_info = self.platforms[platform_name]
self.driver.get(platform_info['login_url'])
try:
WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, platform_info['username_selector']))
)
username_field = self.driver.find_element(By.CSS_SELECTOR, platform_info['username_selector'])
password_field = self.driver.find_element(By.CSS_SELECTOR, platform_info['password_selector'])
submit_button = self.driver.find_element(By.CSS_SELECTOR, platform_info['submit_selector'])
username_field.clear()
username_field.send_keys(username)
time.sleep(1)
password_field.clear()
password_field.send_keys(password)
time.sleep(1)
submit_button.click()
time.sleep(5)
if '验证码' in self.driver.page_source:
print(f"请手动完成 {platform_name} 的验证码验证")
input("按回车键继续...")
self.driver.get(platform_info['url'])
time.sleep(3)
self.cookies[platform_name] = self.driver.get_cookies()
return True
except Exception as e:
print(f"登录 {platform_name} 失败: {str(e)}")
return False
def save_cookies(self, filename='cookies.json'):
with open(filename, 'w') as f:
json.dump(self.cookies, f, indent=4)
print(f"Cookies已保存到 {filename}")
def load_cookies(self, filename='cookies.json'):
if os.path.exists(filename):
with open(filename, 'r') as f:
self.cookies = json.load(f)
print(f"已从 {filename} 加载Cookies")
return True
return False
def set_cookies_to_driver(self, platform_name):
if platform_name not in self.cookies:
print(f"未找到 {platform_name} 的Cookies")
return False
self.driver.get(self.platforms[platform_name]['url'])
time.sleep(2)
for cookie in self.cookies[platform_name]:
if 'expiry' in cookie:
del cookie['expiry']
self.driver.add_cookie(cookie)
self.driver.refresh()
time.sleep(3)
return True
def close(self):
if self.driver:
self.driver.quit()
from cookie_extractor import CookieExtractor import argparse
def main(): parser = argparse.ArgumentParser(description='多平台Cookie提取工具') parser.add_argument('-p', '--platform', help='平台名称: jd/taobao/pdd/douyin', required=True) parser.add_argument('-u', '--username', help='登录用户名', required=True) parser.add_argument('-w', '--password', help='登录密码', required=True) parser.add_argument('-s', '--save', help='保存Cookies到文件', action='store_true') parser.add_argument('-l', '--load', help='从文件加载Cookies', action='store_true') args = parser.parse_args()
extractor = CookieExtractor()
try:
extractor.init_driver()
if args.load:
if extractor.load_cookies():
if extractor.set_cookies_to_driver(args.platform):
print(f"成功使用Cookies登录 {args.platform}")
else:
print(f"使用Cookies登录 {args.platform} 失败")
else:
if extractor.login_platform(args.platform, args.username, args.password):
print(f"成功登录 {args.platform}")
if args.save:
extractor.save_cookies()
except Exception as e:
print(f"发生错误: {str(e)}")
finally:
extractor.close()
if name == 'main': main()