Bläddra i källkod

add 随机等待时间

zhangyong 1 år sedan
förälder
incheckning
8ef9ad70d8

+ 9 - 2
fuqiwang/fuqiwang_recommend/fqw_recommend.py

@@ -1,4 +1,5 @@
 import json
+import random
 import re
 import time
 import requests
@@ -89,6 +90,11 @@ class FqwRecommend:
                                 result = number[0]
                             else:
                                 result = 0
+
+                            time_str = feeds[i]["durationFormat"]
+                            minutes, seconds = map(int, time_str.split(':'))
+                            # 计算总秒数
+                            total_seconds = minutes * 60 + seconds
                             video_dict = {
                                 "video_title": video_title,
                                 "video_id": str(feeds[i]["videoId"]),  # 视频id
@@ -107,7 +113,7 @@ class FqwRecommend:
                                 "like_cnt": 0,
                                 "comment_cnt": 0,
                                 "share_cnt": 0,
-                                # "duration": feeds[i].get("mediaDuration", 0),
+                                "duration": total_seconds,
                                 "session": ""
                             }
                             for k, v in video_dict.items():
@@ -146,7 +152,8 @@ class FqwRecommend:
                                 video_dict["publish_time"] = video_dict["publish_time_str"]
                                 mq.send_msg(video_dict)
                                 cls.download_cnt += 1
-                                time.sleep(5)
+                                interval = random.randrange(5, 11)
+                                time.sleep(interval)
 
                         except Exception as e:
                             Common.logger(log_type, crawler).error(f"抓取单条视频异常:{e}\n")

+ 3 - 1
zhufushenghuo/zhufushenghuo_recommend/zfsh_recommend.py

@@ -1,4 +1,5 @@
 import json
+import random
 import time
 import uuid
 
@@ -184,7 +185,8 @@ class ZfshRecommend:
                                 video_dict["publish_time"] = video_dict["publish_time_str"]
                                 mq.send_msg(video_dict)
                                 cls.download_cnt += 1
-                                time.sleep(5)
+                                interval = random.randrange(5, 11)
+                                time.sleep(interval)
                         except Exception as e:
                             Common.logger(log_type, crawler).error(f"抓取单条视频异常:{e}\n")
                             Common.logging(log_type, crawler, env, f"抓取单条视频异常:{e}\n")