get_imgcode.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import requests
  2. headers = {"accept": "application/json"}
  3. def _pack_file(file):
  4. """包装验证码格式"""
  5. if isinstance(file, str) and file.startswith("data:image"):
  6. img_file = {"file": file}
  7. elif isinstance(file, bytes):
  8. img_file = {"file": file}
  9. else:
  10. with open(file, "rb") as f:
  11. img_bytes = f.read()
  12. img_file = {"file": img_bytes}
  13. return img_file
  14. def _simple_captcha(file):
  15. """
  16. 普通验证码
  17. @param file: 验证码 - 可以是图片或者图片base64编码
  18. @return:
  19. """
  20. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/verify"
  21. files = _pack_file(file)
  22. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  23. rp_json = r.json()
  24. if "msg" in rp_json and "success" == rp_json["msg"]:
  25. return str(rp_json["r"]["code"])
  26. return None
  27. def _arithmetic_captcha(file):
  28. """算术验证码"""
  29. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/arithmetic"
  30. files = _pack_file(file)
  31. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  32. json_resp = r.json()
  33. if "msg" in json_resp and "success" == json_resp["msg"]:
  34. return str(json_resp["r"]["code"])
  35. return None
  36. def _get_click_verify_captcha(file):
  37. """点触式验证码"""
  38. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/verify_det"
  39. files = _pack_file(file)
  40. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  41. return r.json()
  42. def swordfish_platform(file, mode="simple"):
  43. """剑鱼验证码识别平台"""
  44. if mode.lower() == "arithmetic":
  45. return _arithmetic_captcha(file)
  46. elif mode.lower() == "det":
  47. return _get_click_verify_captcha(file)
  48. else:
  49. return _simple_captcha(file)
  50. def chaojiying_platform(file, pic_type: int, spidercode=None):
  51. """
  52. 超级鹰识别平台
  53. pic_type,详情查询地址: https://www.chaojiying.com/price.html
  54. @param str spidercode: 爬虫代码
  55. """
  56. files = _pack_file(file)
  57. url = f"http://pycaptcha.spdata.jianyu360.com/v1/images/discern?pic_type={pic_type}"
  58. if spidercode is not None:
  59. url = f"http://pycaptcha.spdata.jianyu360.com/v1/images/discern?pic_type={pic_type}&jy_code={spidercode}"
  60. headers = {'accept': 'application/json'}
  61. data = {
  62. 'grant_type': '',
  63. 'username': 'jianyu001',
  64. 'password': '123qwe!A',
  65. 'scope': '',
  66. 'client_id': '',
  67. 'client_secret': ''
  68. }
  69. response = requests.post(url, headers=headers, data=data, files=files, timeout=20)
  70. json_resp = response.json()
  71. '''code 返回0时,打码平台正常返回数据'''
  72. pic_str = json_resp["r"]["pic_str"]
  73. pic_id = json_resp["r"]["pic_id"]
  74. if 0 == json_resp["code"]:
  75. return pic_str, pic_id
  76. return None, pic_id
  77. def chaojiying_report(pic_id: str):
  78. """超级鹰平台识别验证码错误时,提交识别错误的验证码pic_id"""
  79. url = f"http://pycaptcha.spdata.jianyu360.com/v1/images/report_err?pic_id={pic_id}"
  80. headers = {
  81. 'accept': 'application/json',
  82. 'Content-Type': 'application/x-www-form-urlencoded'
  83. }
  84. data = {
  85. 'grant_type': '',
  86. 'username': 'jianyu001',
  87. 'password': '123qwe!A',
  88. 'scope': '',
  89. 'client_id': '',
  90. 'client_secret': ''
  91. }
  92. response = requests.post(url, headers=headers, data=data, timeout=10)
  93. '''
  94. 回调成功:{'msg': 'OK', 'code': 0}
  95. 此接口不能随便调用!程序逻辑里要这样判断: 如果 识别结果是错的 再调用 报错返分 接口。 如果没有这个判断或是无法判断,就不要调用!
  96. '''
  97. return response.json()
  98. def get_code(file_path: str) -> dict:
  99. return swordfish_platform(file_path) or {}
  100. def get_code_det(image_bytes) -> dict:
  101. return swordfish_platform(image_bytes, mode="det")
  102. # 算术
  103. def arithmetic_captcha(image_stream):
  104. return swordfish_platform(image_stream, mode="arithmetic")