dongzhaorui@topnet.net.cn 3 rokov pred
rodič
commit
7ccf1591fc
1 zmenil súbory, kde vykonal 16 pridanie a 19 odobranie
  1. 16 19
      ybw/crawler/login.py

+ 16 - 19
ybw/crawler/login.py

@@ -1,5 +1,5 @@
+import io
 import json
-import sys
 import threading
 import time
 import uuid
@@ -11,14 +11,9 @@ import requests
 from requests import Session
 from requests.utils import dict_from_cookiejar
 
-from config.load import node_module
+from config.load import node_module_path
 from utils.log import logger
 
-if sys.platform == 'linux':
-    node_module_path = node_module['linux']
-else:
-    node_module_path = node_module['windows']
-
 LOCK = threading.RLock()
 
 ROOT_PATH = Path(__file__).parent.parent
@@ -116,18 +111,16 @@ def convert1(plaintext):
     return ctx.call('convert1', plaintext)
 
 
-def recognition_captcha(image):
+def recognition_captcha(image_stream):
     """
     验证码识别
 
-    @param image: 验证码图片名称或者路径
+    @param image_stream: 验证码图片流
     @return:
     """
     url = "http://123.57.163.80:2119/v1/images/verify"
     img_headers = {'accept': 'application/json'}
-    with open(image, 'rb') as f:
-        image_bytes = f.read()
-    image_file = {'file': image_bytes}
+    image_file = {'file': image_stream}
     r = requests.post(url, headers=img_headers, files=image_file, stream=True)
     json_resp = r.json()
     if "msg" in json_resp and "success" == json_resp["msg"]:
@@ -135,7 +128,7 @@ def recognition_captcha(image):
     return None
 
 
-def download_captcha(image, session: Session):
+def download_captcha(image, session: Session, save_to_local=False):
     """下载验证码"""
     js_str = '''
         function changeYzmL() {
@@ -153,9 +146,13 @@ def download_captcha(image, session: Session):
         'Referer': 'https://www.chinabidding.cn/public/2020/html/login.html?source=1',
     }
     r = session.get(url, headers=headers, stream=True)
-    with open(image, 'wb') as f:
-        f.write(r.content)
-    logger.info('验证码下载成功')
+    stream = io.BytesIO()
+    stream.write(r.content)
+    if save_to_local:
+        with open(image, 'wb') as f:
+            f.write(r.content)
+    logger.info(f'[验证码]{image} 下载成功')
+    return stream
 
 
 def captcha(session, phone):
@@ -166,9 +163,9 @@ def captcha(session, phone):
     @return:
     """
     name = f'{phone}.jpg'
-    download_captcha(name, session)
-    code = recognition_captcha(name)
-    logger.info(f'验证码识别: {code}')
+    img_stream = download_captcha(name, session)
+    code = recognition_captcha(img_stream.getvalue())
+    logger.info(f'[验证码识别]{code}')
     return convert1(code)