|
@@ -168,135 +168,137 @@ class XiaoNianGaoPlusRecommend:
|
|
|
element_list = [i for i in video_list][index:]
|
|
|
return element_list[0]
|
|
|
|
|
|
- def get_video_info(self, video_element):
|
|
|
- try:
|
|
|
- Common.logger(self.log_type, self.crawler).info(f"本轮已抓取{self.download_cnt}条视频\n")
|
|
|
- # Common.logging(self.log_type, self.crawler, self.env, f"本轮已抓取{self.download_cnt}条视频\n")
|
|
|
- if self.download_cnt >= int(self.rule_dict.get("videos_cnt", {}).get("min", 10)):
|
|
|
- self.count = 0
|
|
|
- self.download_cnt = 0
|
|
|
- self.element_list = []
|
|
|
- return
|
|
|
- self.count += 1
|
|
|
- Common.logger(self.log_type, self.crawler).info(f"第{self.count}条视频")
|
|
|
- # Common.logging(self.log_type, self.crawler, self.env, f"第{self.count}条视频")
|
|
|
- # 标题
|
|
|
- video_title = video_element.find("wx-view", class_="dynamic--title").text
|
|
|
- # 播放量字符串
|
|
|
- play_str = video_element.find("wx-view", class_="dynamic--views").text
|
|
|
- info_list = video_element.findAll("wx-view", class_="dynamic--commerce-btn-text")
|
|
|
- # 点赞数量
|
|
|
- like_str = info_list[1].text
|
|
|
- # 评论数量
|
|
|
- comment_str = info_list[2].text
|
|
|
- # 视频时长
|
|
|
- duration_str = video_element.find("wx-view", class_="dynamic--duration").text
|
|
|
- user_name = video_element.find("wx-view", class_="dynamic--nick-top").text
|
|
|
- # 头像 URL
|
|
|
- avatar_url = video_element.find("wx-image", class_="avatar--avatar")["src"]
|
|
|
- # 封面 URL
|
|
|
- cover_url = video_element.find("wx-image", class_="dynamic--bg-image")["src"]
|
|
|
- play_cnt = int(play_str.replace("+", "").replace("次播放", ""))
|
|
|
- duration = int(duration_str.split(":")[0].strip()) * 60 + int(duration_str.split(":")[-1].strip())
|
|
|
- if "点赞" in like_str:
|
|
|
- like_cnt = 0
|
|
|
- elif "万" in like_str:
|
|
|
- like_cnt = int(like_str.split("万")[0]) * 10000
|
|
|
- else:
|
|
|
- like_cnt = int(like_str)
|
|
|
- if "评论" in comment_str:
|
|
|
- comment_cnt = 0
|
|
|
- elif "万" in comment_str:
|
|
|
- comment_cnt = int(comment_str.split("万")[0]) * 10000
|
|
|
- else:
|
|
|
- comment_cnt = int(comment_str)
|
|
|
- out_video_id = md5(video_title.encode('utf8')).hexdigest()
|
|
|
- out_user_id = md5(user_name.encode('utf8')).hexdigest()
|
|
|
-
|
|
|
- video_dict = {
|
|
|
- "video_title": video_title,
|
|
|
- "video_id": out_video_id,
|
|
|
- "duration_str": duration_str,
|
|
|
- "duration": duration,
|
|
|
- "play_str": play_str,
|
|
|
- "play_cnt": play_cnt,
|
|
|
- "like_str": like_str,
|
|
|
- "like_cnt": like_cnt,
|
|
|
- "comment_cnt": comment_cnt,
|
|
|
- "share_cnt": 0,
|
|
|
- "user_name": user_name,
|
|
|
- "user_id": out_user_id,
|
|
|
- 'publish_time_stamp': int(time.time()),
|
|
|
- 'publish_time_str': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))),
|
|
|
- "avatar_url": avatar_url,
|
|
|
- "cover_url": cover_url,
|
|
|
- "session": f"xiaoniangao-{int(time.time())}"
|
|
|
- }
|
|
|
- for k, v in video_dict.items():
|
|
|
- Common.logger(self.log_type, self.crawler).info(f"{k}:{v}")
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, f"video_dict:{video_dict}")
|
|
|
- # Common.logger(self.log_type, self.crawler).info(f"==========分割线==========\n")
|
|
|
+ def get_video_info_2(self, video_element):
|
|
|
+ Common.logger(self.log_type, self.crawler).info(f"本轮已抓取{self.download_cnt}条视频\n")
|
|
|
+ # Common.logging(self.log_type, self.crawler, self.env, f"本轮已抓取{self.download_cnt}条视频\n")
|
|
|
+ if self.download_cnt >= int(self.rule_dict.get("videos_cnt", {}).get("min", 10)):
|
|
|
+ self.count = 0
|
|
|
+ self.download_cnt = 0
|
|
|
+ self.element_list = []
|
|
|
+ return
|
|
|
+ self.count += 1
|
|
|
+ Common.logger(self.log_type, self.crawler).info(f"第{self.count}条视频")
|
|
|
+ # Common.logging(self.log_type, self.crawler, self.env, f"第{self.count}条视频")
|
|
|
+ # 标题
|
|
|
+ video_title = video_element.find("wx-view", class_="dynamic--title").text
|
|
|
+ # 播放量字符串
|
|
|
+ play_str = video_element.find("wx-view", class_="dynamic--views").text
|
|
|
+ info_list = video_element.findAll("wx-view", class_="dynamic--commerce-btn-text")
|
|
|
+ # 点赞数量
|
|
|
+ like_str = info_list[1].text
|
|
|
+ # 评论数量
|
|
|
+ comment_str = info_list[2].text
|
|
|
+ # 视频时长
|
|
|
+ duration_str = video_element.find("wx-view", class_="dynamic--duration").text
|
|
|
+ user_name = video_element.find("wx-view", class_="dynamic--nick-top").text
|
|
|
+ # 头像 URL
|
|
|
+ avatar_url = video_element.find("wx-image", class_="avatar--avatar")["src"]
|
|
|
+ # 封面 URL
|
|
|
+ cover_url = video_element.find("wx-image", class_="dynamic--bg-image")["src"]
|
|
|
+ play_cnt = int(play_str.replace("+", "").replace("次播放", ""))
|
|
|
+ duration = int(duration_str.split(":")[0].strip()) * 60 + int(duration_str.split(":")[-1].strip())
|
|
|
+ if "点赞" in like_str:
|
|
|
+ like_cnt = 0
|
|
|
+ elif "万" in like_str:
|
|
|
+ like_cnt = int(like_str.split("万")[0]) * 10000
|
|
|
+ else:
|
|
|
+ like_cnt = int(like_str)
|
|
|
+ if "评论" in comment_str:
|
|
|
+ comment_cnt = 0
|
|
|
+ elif "万" in comment_str:
|
|
|
+ comment_cnt = int(comment_str.split("万")[0]) * 10000
|
|
|
+ else:
|
|
|
+ comment_cnt = int(comment_str)
|
|
|
+ out_video_id = md5(video_title.encode('utf8')).hexdigest()
|
|
|
+ out_user_id = md5(user_name.encode('utf8')).hexdigest()
|
|
|
|
|
|
- if video_title is None or cover_url is None:
|
|
|
- Common.logger(self.log_type, self.crawler).info("无效视频\n")
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, '无效视频\n')
|
|
|
- # self.swipe_up()
|
|
|
- time.sleep(0.5)
|
|
|
- elif download_rule(log_type=self.log_type,
|
|
|
- crawler=self.crawler,
|
|
|
- video_dict=video_dict,
|
|
|
- rule_dict=self.rule_dict) is False:
|
|
|
- Common.logger(self.log_type, self.crawler).info("不满足抓取规则\n")
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, "不满足抓取规则\n")
|
|
|
- # self.swipe_up()
|
|
|
- time.sleep(0.5)
|
|
|
- elif any(str(word) if str(word) in video_dict["video_title"] else False
|
|
|
- for word in get_config_from_mysql(log_type=self.log_type,
|
|
|
- source=self.crawler,
|
|
|
- env=self.env,
|
|
|
- text="filter",
|
|
|
- action="")) is True:
|
|
|
- Common.logger(self.log_type, self.crawler).info('已中过滤词\n')
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, '已中过滤词\n')
|
|
|
- # self.swipe_up()
|
|
|
- time.sleep(0.5)
|
|
|
- elif self.repeat_video(out_video_id) != 0:
|
|
|
- Common.logger(self.log_type, self.crawler).info('视频已下载\n')
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, '视频已下载\n')
|
|
|
- # self.swipe_up()
|
|
|
- time.sleep(5)
|
|
|
- else:
|
|
|
- video_title_element = self.search_elements(f'//*[contains(text(), "{video_title}")]')
|
|
|
- if video_title_element is None:
|
|
|
- Common.logger(self.log_type, self.crawler).warning(
|
|
|
- f"未找到该视频标题的element:{video_title_element}")
|
|
|
- Common.logging(self.log_type, self.crawler, self.env,
|
|
|
- f"未找到该视频标题的element:{video_title_element}")
|
|
|
- # continue
|
|
|
- return
|
|
|
- Common.logger(self.log_type, self.crawler).info("点击标题,进入视频详情页")
|
|
|
- Common.logging(self.log_type, self.crawler, self.env, "点击标题,进入视频详情页")
|
|
|
- video_url = self.get_video_url(video_title_element)
|
|
|
- if video_url is None:
|
|
|
- Common.logger(self.log_type, self.crawler).info("未获取到视频播放地址\n")
|
|
|
- self.driver.press_keycode(AndroidKey.BACK)
|
|
|
- time.sleep(5)
|
|
|
- return
|
|
|
- video_dict['video_url'] = video_url
|
|
|
- Common.logger(self.log_type, self.crawler).info(f"video_url:{video_url}")
|
|
|
+ video_dict = {
|
|
|
+ "video_title": video_title,
|
|
|
+ "video_id": out_video_id,
|
|
|
+ "duration_str": duration_str,
|
|
|
+ "duration": duration,
|
|
|
+ "play_str": play_str,
|
|
|
+ "play_cnt": play_cnt,
|
|
|
+ "like_str": like_str,
|
|
|
+ "like_cnt": like_cnt,
|
|
|
+ "comment_cnt": comment_cnt,
|
|
|
+ "share_cnt": 0,
|
|
|
+ "user_name": user_name,
|
|
|
+ "user_id": out_user_id,
|
|
|
+ 'publish_time_stamp': int(time.time()),
|
|
|
+ 'publish_time_str': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()))),
|
|
|
+ "avatar_url": avatar_url,
|
|
|
+ "cover_url": cover_url,
|
|
|
+ "session": f"xiaoniangao-{int(time.time())}"
|
|
|
+ }
|
|
|
+ for k, v in video_dict.items():
|
|
|
+ Common.logger(self.log_type, self.crawler).info(f"{k}:{v}")
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, f"video_dict:{video_dict}")
|
|
|
+ # Common.logger(self.log_type, self.crawler).info(f"==========分割线==========\n")
|
|
|
|
|
|
- video_dict["platform"] = self.crawler
|
|
|
- video_dict["strategy"] = self.log_type
|
|
|
- video_dict["out_video_id"] = video_dict["video_id"]
|
|
|
- video_dict["crawler_rule"] = json.dumps(self.rule_dict)
|
|
|
- video_dict["user_id"] = self.our_uid
|
|
|
- video_dict["publish_time"] = video_dict["publish_time_str"]
|
|
|
- self.mq.send_msg(video_dict)
|
|
|
- # print(video_dict)
|
|
|
- self.download_cnt += 1
|
|
|
+ if video_title is None or cover_url is None:
|
|
|
+ Common.logger(self.log_type, self.crawler).info("无效视频\n")
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, '无效视频\n')
|
|
|
+ # self.swipe_up()
|
|
|
+ time.sleep(0.5)
|
|
|
+ elif download_rule(log_type=self.log_type,
|
|
|
+ crawler=self.crawler,
|
|
|
+ video_dict=video_dict,
|
|
|
+ rule_dict=self.rule_dict) is False:
|
|
|
+ Common.logger(self.log_type, self.crawler).info("不满足抓取规则\n")
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, "不满足抓取规则\n")
|
|
|
+ # self.swipe_up()
|
|
|
+ time.sleep(0.5)
|
|
|
+ elif any(str(word) if str(word) in video_dict["video_title"] else False
|
|
|
+ for word in get_config_from_mysql(log_type=self.log_type,
|
|
|
+ source=self.crawler,
|
|
|
+ env=self.env,
|
|
|
+ text="filter",
|
|
|
+ action="")) is True:
|
|
|
+ Common.logger(self.log_type, self.crawler).info('已中过滤词\n')
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, '已中过滤词\n')
|
|
|
+ # self.swipe_up()
|
|
|
+ time.sleep(0.5)
|
|
|
+ elif self.repeat_video(out_video_id) != 0:
|
|
|
+ Common.logger(self.log_type, self.crawler).info('视频已下载\n')
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, '视频已下载\n')
|
|
|
+ # self.swipe_up()
|
|
|
+ time.sleep(5)
|
|
|
+ else:
|
|
|
+ video_title_element = self.search_elements(f'//*[contains(text(), "{video_title}")]')
|
|
|
+ if video_title_element is None:
|
|
|
+ Common.logger(self.log_type, self.crawler).warning(
|
|
|
+ f"未找到该视频标题的element:{video_title_element}")
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env,
|
|
|
+ f"未找到该视频标题的element:{video_title_element}")
|
|
|
+ # continue
|
|
|
+ return
|
|
|
+ Common.logger(self.log_type, self.crawler).info("点击标题,进入视频详情页")
|
|
|
+ Common.logging(self.log_type, self.crawler, self.env, "点击标题,进入视频详情页")
|
|
|
+ video_url = self.get_video_url(video_title_element)
|
|
|
+ if video_url is None:
|
|
|
+ Common.logger(self.log_type, self.crawler).info("未获取到视频播放地址\n")
|
|
|
self.driver.press_keycode(AndroidKey.BACK)
|
|
|
time.sleep(5)
|
|
|
- # self.swipe_up()
|
|
|
+ return
|
|
|
+ video_dict['video_url'] = video_url
|
|
|
+ Common.logger(self.log_type, self.crawler).info(f"video_url:{video_url}")
|
|
|
+
|
|
|
+ video_dict["platform"] = self.crawler
|
|
|
+ video_dict["strategy"] = self.log_type
|
|
|
+ video_dict["out_video_id"] = video_dict["video_id"]
|
|
|
+ video_dict["crawler_rule"] = json.dumps(self.rule_dict)
|
|
|
+ video_dict["user_id"] = self.our_uid
|
|
|
+ video_dict["publish_time"] = video_dict["publish_time_str"]
|
|
|
+ self.mq.send_msg(video_dict)
|
|
|
+ # print(video_dict)
|
|
|
+ self.download_cnt += 1
|
|
|
+ self.driver.press_keycode(AndroidKey.BACK)
|
|
|
+ time.sleep(5)
|
|
|
+
|
|
|
+ def get_video_info(self, video_element):
|
|
|
+ try:
|
|
|
+ self.get_video_info_2(video_element)
|
|
|
except Exception as e:
|
|
|
Common.logger(self.log_type, self.crawler).error(f"抓取单条视频异常:{e}\n")
|
|
|
# Common.logging(self.log_type, self.crawler, self.env, f"抓取单条视频异常:{e}\n")
|
|
@@ -338,7 +340,7 @@ def run():
|
|
|
rule_dict1 = {"period": {"min": 365, "max": 365},
|
|
|
"duration": {"min": 30, "max": 1800},
|
|
|
"favorite_cnt": {"min": 0, "max": 0},
|
|
|
- "videos_cnt": {"min": 10000, "max": 0},
|
|
|
+ "videos_cnt": {"min": 5000, "max": 0},
|
|
|
"share_cnt": {"min": 0, "max": 0}}
|
|
|
XiaoNianGaoPlusRecommend("recommend", "xiaoniangao", "dev", rule_dict1, 6267141)
|
|
|
|