get_veri_code_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from DrissionPage import ChromiumOptions, Chromium
  2. from DrissionPage.common import Keys
  3. import time
  4. import re
  5. import sys
  6. import os
  7. def get_extension_path():
  8. """获取插件路径"""
  9. root_dir = os.getcwd()
  10. extension_path = os.path.join(root_dir, "turnstilePatch")
  11. if hasattr(sys, "_MEIPASS"):
  12. print("运行在打包环境中")
  13. extension_path = os.path.join(sys._MEIPASS, "turnstilePatch")
  14. print(f"尝试加载插件路径: {extension_path}")
  15. if not os.path.exists(extension_path):
  16. raise FileNotFoundError(
  17. f"插件不存在: {extension_path}\n请确保 turnstilePatch 文件夹在正确位置"
  18. )
  19. return extension_path
  20. def get_browser_options():
  21. co = ChromiumOptions()
  22. try:
  23. extension_path = get_extension_path()
  24. co.add_extension(extension_path)
  25. except FileNotFoundError as e:
  26. print(f"警告: {e}")
  27. co.set_user_agent(
  28. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36"
  29. )
  30. co.set_pref("credentials_enable_service", False)
  31. co.set_argument("--hide-crash-restore-bubble")
  32. co.auto_port()
  33. # Mac 系统特殊处理
  34. if sys.platform == "darwin":
  35. co.set_argument("--no-sandbox")
  36. co.set_argument("--disable-gpu")
  37. return co
  38. def get_veri_code(username):
  39. # 使用相同的浏览器配置
  40. co = get_browser_options()
  41. browser = Chromium(co)
  42. code = None
  43. try:
  44. # 获取当前标签页
  45. tab = browser.latest_tab
  46. tab.run_js("try { turnstile.reset() } catch(e) { }")
  47. # 打开临时邮箱网站
  48. tab.get("https://tempmail.plus/zh")
  49. time.sleep(2)
  50. # 设置邮箱用户名
  51. while True:
  52. if tab.ele("@id=pre_button"):
  53. # 点击输入框
  54. tab.actions.click("@id=pre_button")
  55. time.sleep(1)
  56. # 删除之前的内容
  57. tab.run_js('document.getElementById("pre_button").value = ""')
  58. # 输入新用户名并回车
  59. tab.actions.input(username).key_down(Keys.ENTER).key_up(Keys.ENTER)
  60. break
  61. time.sleep(1)
  62. # 等待并获取新邮件
  63. while True:
  64. new_mail = tab.ele("@class=mail")
  65. if new_mail:
  66. if new_mail.text:
  67. print("最新的邮件:", new_mail.text)
  68. tab.actions.click("@class=mail")
  69. break
  70. else:
  71. print(new_mail)
  72. break
  73. time.sleep(1)
  74. # 提取验证码
  75. if tab.ele("@class=overflow-auto mb-20"):
  76. email_content = tab.ele("@class=overflow-auto mb-20").text
  77. verification_code = re.search(
  78. r"verification code is (\d{6})", email_content
  79. )
  80. if verification_code:
  81. code = verification_code.group(1)
  82. print("验证码:", code)
  83. else:
  84. print("未找到验证码")
  85. # 删除邮件
  86. if tab.ele("@id=delete_mail"):
  87. tab.actions.click("@id=delete_mail")
  88. time.sleep(1)
  89. if tab.ele("@id=confirm_mail"):
  90. tab.actions.click("@id=confirm_mail")
  91. print("删除邮件")
  92. except Exception as e:
  93. print(f"发生错误: {str(e)}")
  94. finally:
  95. browser.quit()
  96. return code
  97. # 测试运行
  98. if __name__ == "__main__":
  99. test_username = "test_user" # 替换为你要测试的用户名
  100. code = get_veri_code(test_username)
  101. print(f"获取到的验证码: {code}")