爬取结果异常处理
Showing
2 changed files
with
6 additions
and
3 deletions
... | @@ -69,9 +69,13 @@ def get_job_info(taskId: int): | ... | @@ -69,9 +69,13 @@ def get_job_info(taskId: int): |
69 | 69 | ||
70 | 70 | ||
71 | async def scrawl_and_save(taskParam: SpiderParams): | 71 | async def scrawl_and_save(taskParam: SpiderParams): |
72 | # 执行爬虫获取结果 | 72 | try: |
73 | # 执行爬虫获取结果 给下面一行代码添加 try cache try 捕获异常 | ||
73 | results = await startBrowser(taskParam) | 74 | results = await startBrowser(taskParam) |
75 | except Exception as e: | ||
74 | logger.info(f"爬虫重试情况:{startBrowser.statistics}") | 76 | logger.info(f"爬虫重试情况:{startBrowser.statistics}") |
77 | logger.error(f"爬虫任务执行失败,失败原因:{e}") | ||
78 | return | ||
75 | asyncTasks = (save_or_update(item) for item in results) | 79 | asyncTasks = (save_or_update(item) for item in results) |
76 | await asyncio.gather(*asyncTasks) | 80 | await asyncio.gather(*asyncTasks) |
77 | logger.info(f"爬虫任务执行完成,爬取到数据{len(results)}条 保存到数据库完成") | 81 | logger.info(f"爬虫任务执行完成,爬取到数据{len(results)}条 保存到数据库完成") | ... | ... |
... | @@ -12,8 +12,7 @@ from tenacity import ( | ... | @@ -12,8 +12,7 @@ from tenacity import ( |
12 | before_sleep_log, | 12 | before_sleep_log, |
13 | retry, | 13 | retry, |
14 | stop_after_attempt, | 14 | stop_after_attempt, |
15 | wait_exponential, | 15 | wait_exponential |
16 | wait_fixed, | ||
17 | ) | 16 | ) |
18 | 17 | ||
19 | logger = logging.getLogger(__name__) | 18 | logger = logging.getLogger(__name__) | ... | ... |
-
Please register or sign in to post a comment