更改为loguru, 增加随机ua, 增加隐式等待

This commit is contained in:
work
2025-05-26 17:08:38 +08:00
parent 24722b4363
commit aa4a627db8
2 changed files with 114 additions and 68 deletions

23
main.py
View File

@@ -1,6 +1,7 @@
from concurrent.futures.thread import ThreadPoolExecutor
from spider.task import *
from logger import error_logger, record_full_log
from loguru import logger
logger.add("./log/logging.log", rotation="50 MB")
TASK_TYPE = {
'get_account_profile': playwright_get_user_profile,
@@ -23,10 +24,10 @@ def get_task():
'Content-Type': 'application/json'
}
data = {
"include_task_type": ["login_account"],
"include_task_type": ["login_account", "get_account_profile"],
"exclude_task_type": []
}
response = requests.post(url, headers=header, json=data)
response = requests.post(url, headers=header, json=data, proxies=None)
if response.status_code == 200:
result = response.json()
return result
@@ -39,13 +40,15 @@ def task_callback(tid, data, status='success', msg='success'):
'data': data,
'message': msg,
}
logger.info(f"回调任务: tid:{tid}, status:{status}, data:{data}, msg:{msg}")
response = requests.post(
f'{HOST}/queue/handle-data',
json=body
json=body,
proxies=None
)
result = response.json()
if response.status_code != 200:
raise RuntimeError(f"任务回调失败:{result['msg']}")
raise RuntimeError(f"任务回调失败:code={response.status_code} text={result.text}")
def execute_task(tid, task_type, **kwargs):
@@ -53,10 +56,10 @@ def execute_task(tid, task_type, **kwargs):
result = TASK_TYPE.get(task_type)(**kwargs)
task_callback(tid, data=result)
except (AuthException, OperationFailed) as e:
record_full_log(error_logger, e)
logger.exception("账号或操作异常")
task_callback(tid, data={}, status=e.error_type, msg=str(e))
except Exception as e:
record_full_log(error_logger, e)
logger.exception("未捕获异常")
task_callback(tid, data={}, status='failed', msg=str(e))
@@ -65,13 +68,15 @@ def main():
try:
task = get_task()
if task is None:
logger.info("无任务")
time.sleep(10)
continue
logger.info(f"收到任务{task}")
task['data']['tid'] = task['id']
task['data']['task_type'] = task['task_type']
execute_task(**task['data'])
except Exception as e:
error_logger.error(f'Main Error: {e}')
logger.error(f'Main Error: {e}')
time.sleep(10)