|
@@ -37,7 +37,7 @@ class ZFQZRecommend:
|
|
|
caps = {
|
|
|
"platformName": "Android",
|
|
|
"devicesName": "Android",
|
|
|
- "platformVersion": "12",
|
|
|
+ "platformVersion": "11",
|
|
|
# "udid": "emulator-5554",
|
|
|
"appPackage": "com.tencent.mm",
|
|
|
"appActivity": ".ui.LauncherUI",
|
|
@@ -161,112 +161,116 @@ class ZFQZRecommend:
|
|
|
return
|
|
|
|
|
|
for i, video_element in enumerate(video_list):
|
|
|
- if cls.download_cnt >= int(rule_dict.get("videos_cnt", {}).get("min", 10)):
|
|
|
- Common.logger(log_type, crawler).info(f"本轮已抓取视频数:{cls.download_cnt}")
|
|
|
- Common.logging(log_type, crawler, env, f"本轮已抓取视频数:{cls.download_cnt}")
|
|
|
- return
|
|
|
+ try:
|
|
|
+ if cls.download_cnt >= int(rule_dict.get("videos_cnt", {}).get("min", 10)):
|
|
|
+ Common.logger(log_type, crawler).info(f"本轮已抓取视频数:{cls.download_cnt}")
|
|
|
+ Common.logging(log_type, crawler, env, f"本轮已抓取视频数:{cls.download_cnt}")
|
|
|
+ return
|
|
|
|
|
|
- if video_element is None:
|
|
|
- Common.logger(log_type, crawler).info("没有更多数据啦~\n")
|
|
|
- Common.logging(log_type, crawler, env, "没有更多数据啦~\n")
|
|
|
- return
|
|
|
+ if video_element is None:
|
|
|
+ Common.logger(log_type, crawler).info("没有更多数据啦~\n")
|
|
|
+ Common.logging(log_type, crawler, env, "没有更多数据啦~\n")
|
|
|
+ return
|
|
|
|
|
|
- cls.i += 1
|
|
|
- cls.search_elements(driver, '//*[@is="pages/discover/components/bless/dynamic/dynamic"]')
|
|
|
- Common.logger(log_type, crawler).info(f"拖动第{cls.i}条视频至屏幕中间")
|
|
|
- Common.logging(log_type, crawler, env, f"拖动第{cls.i}条视频至屏幕中间")
|
|
|
- time.sleep(3)
|
|
|
- driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})",
|
|
|
- video_element)
|
|
|
+ cls.i += 1
|
|
|
+ cls.search_elements(driver, '//*[@is="pages/discover/components/bless/dynamic/dynamic"]')
|
|
|
+ Common.logger(log_type, crawler).info(f"拖动第{cls.i}条视频至屏幕中间")
|
|
|
+ Common.logging(log_type, crawler, env, f"拖动第{cls.i}条视频至屏幕中间")
|
|
|
+ time.sleep(3)
|
|
|
+ driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'})",
|
|
|
+ video_element)
|
|
|
|
|
|
- video_title = video_element.find_elements(By.XPATH, '//*[@class="dynamic--title"]')[index+i].text
|
|
|
- play_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--views"]')[index + i].text
|
|
|
- duration_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--duration"]')[index + i].text
|
|
|
- user_name = video_element.find_elements(By.XPATH, '//*[@class="dynamic--nick-top"]')[index + i].text
|
|
|
- like_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--commerce"]/*[1]/*[2]')[index + i].text
|
|
|
- comment_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--commerce"]/*[2]/*[2]')[index + i].text
|
|
|
- cover_url = video_element.find_elements(By.XPATH, '//*[@class="dynamic--bg-image"]')[index+i].get_attribute('src')
|
|
|
- avatar_url = video_element.find_elements(By.XPATH, '//*[@class="avatar--avatar"]')[index+i].get_attribute('src')
|
|
|
+ video_title = video_element.find_elements(By.XPATH, '//*[@class="dynamic--title"]')[index+i].text
|
|
|
+ play_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--views"]')[index + i].text
|
|
|
+ duration_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--duration"]')[index + i].text
|
|
|
+ user_name = video_element.find_elements(By.XPATH, '//*[@class="dynamic--nick-top"]')[index + i].text
|
|
|
+ like_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--commerce"]/*[1]/*[2]')[index + i].text
|
|
|
+ comment_cnt_str = video_element.find_elements(By.XPATH, '//*[@class="dynamic--commerce"]/*[2]/*[2]')[index + i].text
|
|
|
+ cover_url = video_element.find_elements(By.XPATH, '//*[@class="dynamic--bg-image"]')[index+i].get_attribute('src')
|
|
|
+ avatar_url = video_element.find_elements(By.XPATH, '//*[@class="avatar--avatar"]')[index+i].get_attribute('src')
|
|
|
|
|
|
- play_cnt = int(play_cnt_str.replace("+", "").replace("次播放", ""))
|
|
|
- duration = int(duration_str.split(":")[0].strip())*60 + int(duration_str.split(":")[-1].strip())
|
|
|
- if "点赞" in like_cnt_str:
|
|
|
- like_cnt = 0
|
|
|
- elif "万" in like_cnt_str:
|
|
|
- like_cnt = int(like_cnt_str.split("万")[0])*10000
|
|
|
- else:
|
|
|
- like_cnt = int(like_cnt_str)
|
|
|
- if "评论" in comment_cnt_str:
|
|
|
- comment_cnt = 0
|
|
|
- elif "万" in comment_cnt_str:
|
|
|
- comment_cnt = int(comment_cnt_str.split("万")[0])*10000
|
|
|
- else:
|
|
|
- comment_cnt = int(comment_cnt_str)
|
|
|
- out_video_id = md5(video_title.encode('utf8')).hexdigest()
|
|
|
- out_user_id = md5(user_name.encode('utf8')).hexdigest()
|
|
|
+ play_cnt = int(play_cnt_str.replace("+", "").replace("次播放", ""))
|
|
|
+ duration = int(duration_str.split(":")[0].strip())*60 + int(duration_str.split(":")[-1].strip())
|
|
|
+ if "点赞" in like_cnt_str:
|
|
|
+ like_cnt = 0
|
|
|
+ elif "万" in like_cnt_str:
|
|
|
+ like_cnt = int(like_cnt_str.split("万")[0])*10000
|
|
|
+ else:
|
|
|
+ like_cnt = int(like_cnt_str)
|
|
|
+ if "评论" in comment_cnt_str:
|
|
|
+ comment_cnt = 0
|
|
|
+ elif "万" in comment_cnt_str:
|
|
|
+ comment_cnt = int(comment_cnt_str.split("万")[0])*10000
|
|
|
+ else:
|
|
|
+ comment_cnt = int(comment_cnt_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": duration,
|
|
|
- "play_cnt": play_cnt,
|
|
|
- "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"zhufuquanzi-{int(time.time())}"
|
|
|
- }
|
|
|
- for k, v in video_dict.items():
|
|
|
- Common.logger(log_type, crawler).info(f"{k}:{v}")
|
|
|
- Common.logging(log_type, crawler, env, f"video_dict:{video_dict}")
|
|
|
+ video_dict = {
|
|
|
+ "video_title": video_title,
|
|
|
+ "video_id": out_video_id,
|
|
|
+ "duration": duration,
|
|
|
+ "play_cnt": play_cnt,
|
|
|
+ "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"zhufuquanzi-{int(time.time())}"
|
|
|
+ }
|
|
|
+ for k, v in video_dict.items():
|
|
|
+ Common.logger(log_type, crawler).info(f"{k}:{v}")
|
|
|
+ Common.logging(log_type, crawler, env, f"video_dict:{video_dict}")
|
|
|
|
|
|
- if video_title is None or cover_url is None:
|
|
|
- Common.logger(log_type, crawler).info("无效视频\n")
|
|
|
- Common.logging(log_type, crawler, env, '无效视频\n')
|
|
|
- elif download_rule(log_type=log_type, crawler=crawler, video_dict=video_dict,
|
|
|
- rule_dict=rule_dict) is False:
|
|
|
- Common.logger(log_type, crawler).info("不满足抓取规则\n")
|
|
|
- Common.logging(log_type, crawler, env, "不满足抓取规则\n")
|
|
|
- elif any(str(word) if str(word) in video_dict["video_title"] else False
|
|
|
- for word in get_config_from_mysql(log_type=log_type,
|
|
|
- source=crawler,
|
|
|
- env=env,
|
|
|
- text="filter",
|
|
|
- action="")) is True:
|
|
|
- Common.logger(log_type, crawler).info('已中过滤词\n')
|
|
|
- Common.logging(log_type, crawler, env, '已中过滤词\n')
|
|
|
- elif cls.repeat_video(log_type, crawler, out_video_id, env) != 0:
|
|
|
- Common.logger(log_type, crawler).info('视频已下载\n')
|
|
|
- Common.logging(log_type, crawler, env, '视频已下载\n')
|
|
|
- else:
|
|
|
- video_element.click()
|
|
|
- time.sleep(3)
|
|
|
- video_url_elements = cls.search_elements(driver, '//*[@class="index--video-item index--video"]')
|
|
|
- if video_url_elements is None or len(video_url_elements) == 0:
|
|
|
- Common.logger(log_type, crawler).info("未获取到视频播放地址\n")
|
|
|
- Common.logging(log_type, crawler, env, "未获取到视频播放地址\n")
|
|
|
- driver.press_keycode(AndroidKey.BACK)
|
|
|
+ if video_title is None or cover_url is None:
|
|
|
+ Common.logger(log_type, crawler).info("无效视频\n")
|
|
|
+ Common.logging(log_type, crawler, env, '无效视频\n')
|
|
|
+ elif download_rule(log_type=log_type, crawler=crawler, video_dict=video_dict,
|
|
|
+ rule_dict=rule_dict) is False:
|
|
|
+ Common.logger(log_type, crawler).info("不满足抓取规则\n")
|
|
|
+ Common.logging(log_type, crawler, env, "不满足抓取规则\n")
|
|
|
+ elif any(str(word) if str(word) in video_dict["video_title"] else False
|
|
|
+ for word in get_config_from_mysql(log_type=log_type,
|
|
|
+ source=crawler,
|
|
|
+ env=env,
|
|
|
+ text="filter",
|
|
|
+ action="")) is True:
|
|
|
+ Common.logger(log_type, crawler).info('已中过滤词\n')
|
|
|
+ Common.logging(log_type, crawler, env, '已中过滤词\n')
|
|
|
+ elif cls.repeat_video(log_type, crawler, out_video_id, env) != 0:
|
|
|
+ Common.logger(log_type, crawler).info('视频已下载\n')
|
|
|
+ Common.logging(log_type, crawler, env, '视频已下载\n')
|
|
|
else:
|
|
|
- video_url = video_url_elements[0].get_attribute("src")
|
|
|
- video_dict["video_url"] = video_url
|
|
|
- Common.logger(log_type, crawler).info(f"video_url:{video_url}")
|
|
|
+ video_element.click()
|
|
|
+ time.sleep(3)
|
|
|
+ video_url_elements = cls.search_elements(driver, '//*[@class="index--video-item index--video"]')
|
|
|
+ if video_url_elements is None or len(video_url_elements) == 0:
|
|
|
+ Common.logger(log_type, crawler).info("未获取到视频播放地址\n")
|
|
|
+ Common.logging(log_type, crawler, env, "未获取到视频播放地址\n")
|
|
|
+ driver.press_keycode(AndroidKey.BACK)
|
|
|
+ else:
|
|
|
+ video_url = video_url_elements[0].get_attribute("src")
|
|
|
+ video_dict["video_url"] = video_url
|
|
|
+ Common.logger(log_type, crawler).info(f"video_url:{video_url}")
|
|
|
|
|
|
- video_dict["platform"] = crawler
|
|
|
- video_dict["strategy"] = log_type
|
|
|
- video_dict["out_video_id"] = video_dict["video_id"]
|
|
|
- video_dict["crawler_rule"] = json.dumps(rule_dict)
|
|
|
- video_dict["user_id"] = our_uid
|
|
|
- video_dict["publish_time"] = video_dict["publish_time_str"]
|
|
|
- mq.send_msg(video_dict)
|
|
|
- cls.download_cnt += 1
|
|
|
- driver.press_keycode(AndroidKey.BACK)
|
|
|
- Common.logger(log_type, crawler).info("符合抓取条件,mq send msg 成功\n")
|
|
|
- Common.logging(log_type, crawler, env, "符合抓取条件,ACK MQ 成功\n")
|
|
|
+ video_dict["platform"] = crawler
|
|
|
+ video_dict["strategy"] = log_type
|
|
|
+ video_dict["out_video_id"] = video_dict["video_id"]
|
|
|
+ video_dict["crawler_rule"] = json.dumps(rule_dict)
|
|
|
+ video_dict["user_id"] = our_uid
|
|
|
+ video_dict["publish_time"] = video_dict["publish_time_str"]
|
|
|
+ mq.send_msg(video_dict)
|
|
|
+ cls.download_cnt += 1
|
|
|
+ driver.press_keycode(AndroidKey.BACK)
|
|
|
+ Common.logger(log_type, crawler).info("符合抓取条件,mq send msg 成功\n")
|
|
|
+ Common.logging(log_type, crawler, env, "符合抓取条件,ACK MQ 成功\n")
|
|
|
+ except Exception as e:
|
|
|
+ Common.logger(log_type, crawler).error(f"抓取单条视频异常:{e}\n")
|
|
|
+ Common.logging(log_type, crawler, env, f"抓取单条视频异常:{e}\n")
|
|
|
|
|
|
|
|
|
Common.logger(log_type, crawler).info("已抓取完一组,休眠 10 秒\n")
|