gpt-accesstoken.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from DrissionPage import ChromiumOptions, Chromium
  2. import random
  3. import time
  4. def handle_turnstile(tab):
  5. """处理 Turnstile 验证"""
  6. print("准备处理验证")
  7. try:
  8. while True:
  9. if tab.ele('@id=email-input', timeout=2):
  10. print("无需验证 - 邮箱输入框已加载")
  11. return True
  12. if tab.ele('@id=password', timeout=2):
  13. print("无需验证 - 密码输入框已加载")
  14. return True
  15. try:
  16. challenge_element = (tab.ele("@name=cf-turnstile-response", timeout=2)
  17. .parent()
  18. .shadow_root
  19. .ele("tag:iframe")
  20. .ele("tag:body")
  21. .sr("tag:input"))
  22. if challenge_element:
  23. print("验证框加载完成")
  24. time.sleep(random.uniform(1, 3))
  25. challenge_element.click()
  26. print("验证按钮已点击,等待验证完成...")
  27. time.sleep(2)
  28. return True
  29. except:
  30. pass
  31. time.sleep(2)
  32. except Exception as e:
  33. print(f"验证处理出错: {str(e)}")
  34. print('跳过验证')
  35. return False
  36. account = 'your_chatgpt_account'
  37. password = 'your_chatgpt_password'
  38. co = ChromiumOptions()
  39. co.add_extension("turnstilePatch")
  40. # co.headless()
  41. co.set_user_agent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36')
  42. co.set_pref('credentials_enable_service', False)
  43. co.set_argument('--hide-crash-restore-bubble')
  44. co.auto_port()
  45. browser = Chromium(co)
  46. tab = browser.latest_tab
  47. tab.run_js("try { turnstile.reset() } catch(e) { }")
  48. print("\n步骤1: 开始访问网站...")
  49. tab.get('https://chatgpt.com')
  50. print('等待页面加载...')
  51. print("\n步骤2: 开始登录...")
  52. for _ in range(5):
  53. try:
  54. if tab.ele('xpath:/html/body/div[1]/div[1]/main/div[1]/div[1]/div/div[1]/div/div[3]/div/button[1]'):
  55. signin_btn = tab.ele('xpath:/html/body/div[1]/div[1]/main/div[1]/div[1]/div/div[1]/div/div[3]/div/button[1]')
  56. print("找到黑色登录按钮:", signin_btn.text)
  57. break
  58. if tab.ele('@data-testid=login-button'):
  59. signin_btn = tab.ele('@data-testid=login-button')
  60. print("找到蓝色登录按钮:", signin_btn.text)
  61. break
  62. if tab.ele("@name=cf-turnstile-response"):
  63. print('加载页面时出现CF验证, IP 质量太差, 请更换 IP 重新尝试!')
  64. browser.quit()
  65. exit()
  66. time.sleep(3)
  67. except Exception as e:
  68. print(f"处理登录按钮时出错: {str(e)}")
  69. for _ in range(5):
  70. try:
  71. if signin_btn:
  72. signin_btn.click()
  73. print("点击登录按钮")
  74. break
  75. except Exception as e:
  76. print(f"处理登录按钮时出错: {str(e)}")
  77. time.sleep(3)
  78. else:
  79. print("尝试点击登录按钮失败,程序退出")
  80. exit()
  81. handle_turnstile(tab)
  82. print("\n步骤3: 输入邮箱...")
  83. for _ in range(5):
  84. try:
  85. if tab.ele('@id=email-input'):
  86. tab.actions.click('@id=email-input').type(account)
  87. time.sleep(0.5)
  88. tab.ele('@class=continue-btn').click()
  89. print("输入邮箱并点击继续")
  90. break
  91. except Exception as e:
  92. print(f"加载邮箱输入框时出错: {str(e)}")
  93. time.sleep(3)
  94. else:
  95. print("尝试加载邮箱输入框失败,程序退出")
  96. browser.quit()
  97. exit()
  98. handle_turnstile(tab)
  99. print("\n步骤4: 输入密码...")
  100. for _ in range(5):
  101. try:
  102. if tab.ele('@id=password'):
  103. print("密码输入框加载完成")
  104. tab.actions.click('@id=password').input(password)
  105. time.sleep(2)
  106. tab.ele('@type=submit').click('js')
  107. # tab.actions.click('@type=submit')
  108. print("输入密码并JS点击登录")
  109. break
  110. except Exception as e:
  111. print(f"输入密码时出错: {str(e)}")
  112. time.sleep(3)
  113. else:
  114. print("尝试加载密码输入框失败,程序退出")
  115. browser.quit()
  116. exit()
  117. for _ in range(5):
  118. try:
  119. if tab.ele('有什么可以帮忙的?'):
  120. print('登录成功!')
  121. break
  122. if tab.ele('重新发送电子邮件'):
  123. print('提示需要邮箱验证码,脚本终止,请手动获取')
  124. exit()
  125. except Exception as e:
  126. print(f"登录可能遇到问题: {str(e)}")
  127. time.sleep(3)
  128. else:
  129. print("登录失败,程序退出")
  130. browser.quit()
  131. exit()
  132. time.sleep(random.uniform(1,2))
  133. print('\n',"步骤5: 获取access_token...")
  134. browser.new_tab('https://chatgpt.com/api/auth/session')
  135. tab = browser.latest_tab
  136. time.sleep(1)
  137. response_json = tab.json
  138. if response_json and 'accessToken' in response_json:
  139. access_token = response_json['accessToken']
  140. print('\n',"请复制保存你的access_token:",'\n')
  141. print(access_token)
  142. else:
  143. print("错误:未找到access token")
  144. # input("\n按Enter键关闭浏览器...")
  145. browser.quit()