├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # JD_Get_Cookie 2 | 1. 下载[release](https://github.com/Waikkii/JD_Get_Cookie/releases/tag/V2.1)版本 3 | 2. 打开exe 4 | 3. 登录(画线验证码多试几次,3-5次) 5 | 4. 登陆成功后回到命令行复制cookie 6 | 6. 回车关闭程序 7 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from os import path, remove, popen 2 | from sys import argv 3 | from zipfile import ZipFile 4 | from winreg import OpenKey, QueryValueEx, HKEY_CURRENT_USER 5 | from requests import get 6 | from pyperclip import copy 7 | from datetime import datetime 8 | from fake_useragent import UserAgent 9 | from selenium import webdriver 10 | from selenium.webdriver.support import expected_conditions as EC 11 | from selenium.webdriver.support.wait import WebDriverWait 12 | 13 | chrome_options = webdriver.ChromeOptions() 14 | chrome_options.add_argument('log-level=3') 15 | 16 | #去除webdriver特征 17 | chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) 18 | chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) 19 | chrome_options.add_experimental_option('useAutomationExtension', False) 20 | chrome_options.add_argument("--disable-blink-features") 21 | chrome_options.add_argument("--disable-blink-features=AutomationControlled") 22 | 23 | url='https://registry.npmmirror.com/-/binary/chromedriver/' # chromedriver download link 24 | 25 | def exitWait(): 26 | getYourCode = input("请按回车键自动退出。") 27 | exit(0) 28 | 29 | def get_path(): 30 | return path.dirname(path.realpath(argv[0])) 31 | #return path.dirname(path.realpath(__file__)) 32 | 33 | def get_Chrome_version(): 34 | key = OpenKey(HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon') 35 | version, types = QueryValueEx(key, 'version') 36 | return version 37 | 38 | def get_server_chrome_versions(): 39 | '''return all versions list''' 40 | versionList=[] 41 | version_url="https://registry.npmmirror.com/-/binary/chromedriver/" 42 | rep = get(version_url).json() 43 | for i in rep: 44 | version = i["name"].replace("/","") # 提取版本号 45 | versionList.append(version) # 将所有版本存入列表 46 | return versionList 47 | 48 | def download_driver(download_url): 49 | '''下载文件''' 50 | file = get(download_url) 51 | with open("chromedriver.zip", 'wb') as zip_file: # 保存文件到脚本所在目录 52 | zip_file.write(file.content) 53 | print('下载成功') 54 | 55 | def download_lase_driver(download_url, chromeVersion, chrome_main_version): 56 | '''更新driver''' 57 | versionList=get_server_chrome_versions() 58 | if chromeVersion in versionList: 59 | download_url=f"{url}{chromeVersion}/chromedriver_win32.zip" 60 | else: 61 | for version in versionList: 62 | if version.startswith(str(chrome_main_version)): 63 | download_url=f"{url}{version}/chromedriver_win32.zip" 64 | break 65 | if download_url=="": 66 | print("暂无法找到与chrome兼容的chromedriver版本,请前往http://npm.taobao.org/mirrors/chromedriver/ 核实。") 67 | download_driver(download_url=download_url) 68 | path = get_path() 69 | print('当前路径为:', path) 70 | unzip_driver(path) 71 | remove("chromedriver.zip") 72 | dri_version = get_version() 73 | if dri_version == 0: 74 | return 0 75 | else: 76 | print('更新后的Chromedriver版本为:', dri_version) 77 | 78 | def get_version(): 79 | '''查询系统内的Chromedriver版本''' 80 | outstd2 = popen('chromedriver --version').read() 81 | try: 82 | out = outstd2.split(' ')[1] 83 | except: 84 | return 0 85 | return out 86 | 87 | def unzip_driver(path): 88 | '''解压Chromedriver压缩包到指定目录''' 89 | f = ZipFile("chromedriver.zip",'r') 90 | for file in f.namelist(): 91 | f.extract(file, path) 92 | 93 | def check_update_chromedriver(): 94 | try: 95 | chromeVersion=get_Chrome_version() 96 | except: 97 | print('未安装Chrome,请在GooGle Chrome官网:https://www.google.cn/chrome/ 下载。') 98 | return 0 99 | 100 | chrome_main_version=int(chromeVersion.split(".")[0]) # chrome主版本号 101 | 102 | try: 103 | driverVersion=get_version() 104 | driver_main_version=int(driverVersion.split(".")[0]) # chromedriver主版本号 105 | except: 106 | print(f"未安装Chromedriver,正在为您自动下载({chromeVersion})>>>") 107 | download_url="" 108 | if download_lase_driver(download_url, chromeVersion, chrome_main_version) == 0: 109 | return 0 110 | driverVersion=get_version() 111 | driver_main_version=int(driverVersion.split(".")[0]) # chromedriver主版本号 112 | 113 | download_url="" 114 | if driver_main_version!=chrome_main_version: 115 | print(f"chromedriver版本与chrome浏览器不兼容,更新中({chromeVersion})>>>",) 116 | if download_lase_driver(download_url, chromeVersion, chrome_main_version) == 0: 117 | return 0 118 | else: 119 | print(f"chromedriver版本已与chrome浏览器相兼容({chromeVersion}),无需更新chromedriver版本!") 120 | 121 | def find_and_paste(cookie): 122 | # 文件路径 123 | for item in cookie.split('; '): 124 | if 'pt_pin' in item: 125 | pt_pin = item 126 | if 'pt_key' in item: 127 | pt_key = item 128 | jd_cookie = pt_pin+';'+pt_key+';' 129 | copy(jd_cookie) 130 | return jd_cookie 131 | 132 | def main(): 133 | print('请在弹出的网页中登录账号。推荐使用账户密码形式登录。') 134 | 135 | ua = UserAgent().safari 136 | print('随机UA为:', ua) 137 | 138 | driver = webdriver.Chrome(executable_path=get_path()+'\chromedriver.exe', options=chrome_options) 139 | 140 | driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { 141 | "source": """ 142 | Object.defineProperty(navigator, 'webdriver', { 143 | get: () => undefined 144 | }) 145 | """ 146 | }) 147 | 148 | print("当前浏览器内置user-agent为:", driver.execute_script('return navigator.userAgent')) 149 | 150 | driver.execute_cdp_cmd("Emulation.setUserAgentOverride", { 151 | "userAgent": ua 152 | }) 153 | 154 | print("更改后浏览器user-agent为:", driver.execute_script('return navigator.userAgent')) 155 | 156 | driver.get('https://bean.m.jd.com/bean/signIndex.action') 157 | 158 | try: 159 | if WebDriverWait(driver, 600, poll_frequency=0.2, ignored_exceptions=None).until(EC.title_is(u"签到日历")): 160 | '''判断title,返回布尔值''' 161 | nowtime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 162 | print('{} : 登录成功'.format(nowtime)) 163 | 164 | except: 165 | print('超时退出') 166 | exitWait() 167 | exit(2) 168 | 169 | jd_cookies = driver.get_cookies() 170 | 171 | for cookie in jd_cookies: 172 | if cookie['name'] == "pt_key": 173 | pt_key ='{}={};'.format(cookie['name'], cookie['value']) 174 | elif cookie['name'] == "pt_pin": 175 | pt_pin = '{}={};'.format(cookie['name'], cookie['value']) 176 | 177 | result = pt_key + pt_pin 178 | copy(result) 179 | print('jd_cookie: ', result) 180 | input('按Enter键退出...') 181 | driver.close() 182 | 183 | if __name__ == '__main__': 184 | check_update_chromedriver() 185 | main() 186 | --------------------------------------------------------------------------------