|
@@ -35,7 +35,8 @@ class XiaoNianGaoPlusRecommend:
|
|
|
chromedriverExecutable = "/Users/piaoquan/Downloads/chromedriver"
|
|
|
|
|
|
Common.logger(log_type, crawler).info("启动微信")
|
|
|
- # Common.logging(log_type, crawler, env, '启动微信')
|
|
|
+ Common.logging(log_type, crawler, env, '启动微信')
|
|
|
+ # 微信的配置文件
|
|
|
caps = {
|
|
|
"platformName": "Android",
|
|
|
"devicesName": "Android",
|
|
@@ -85,11 +86,7 @@ class XiaoNianGaoPlusRecommend:
|
|
|
# Common.logging(log_type, crawler, env, '打开小程序"小年糕+"')
|
|
|
driver.find_elements(By.XPATH, '//*[@text="小年糕+"]')[-1].click()
|
|
|
time.sleep(5)
|
|
|
-
|
|
|
- # print(driver.contexts)
|
|
|
-
|
|
|
cls.get_videoList(log_type, crawler, driver, env, rule_dict, our_uid)
|
|
|
-
|
|
|
time.sleep(1)
|
|
|
driver.quit()
|
|
|
|
|
@@ -110,52 +107,51 @@ class XiaoNianGaoPlusRecommend:
|
|
|
@classmethod
|
|
|
def check_to_applet(cls, log_type, crawler, env, driver: WebDriver, xpath):
|
|
|
time.sleep(1)
|
|
|
- # print(driver.)
|
|
|
webViews = driver.contexts
|
|
|
- print(webViews)
|
|
|
- # Common.logger(log_type, crawler).info(f"webViews:{webViews}")
|
|
|
- # Common.logging(log_type, crawler, env, f"webViews:{webViews}")
|
|
|
- driver.switch_to.context(webViews[2])
|
|
|
+ driver.switch_to.context(webViews[-1])
|
|
|
windowHandles = driver.window_handles
|
|
|
for handle in windowHandles:
|
|
|
driver.switch_to.window(handle)
|
|
|
time.sleep(1)
|
|
|
try:
|
|
|
driver.find_element(By.XPATH, xpath)
|
|
|
- Common.logger(log_type, crawler).info("切换到小程序成功\n")
|
|
|
- Common.logging(log_type, crawler, env, '切换到小程序成功\n')
|
|
|
+ Common.logger(log_type, crawler).info("切换到WebView成功\n")
|
|
|
+ Common.logging(log_type, crawler, env, '切换到WebView成功\n')
|
|
|
return
|
|
|
except NoSuchElementException:
|
|
|
time.sleep(1)
|
|
|
|
|
|
@classmethod
|
|
|
def repeat_video(cls, log_type, crawler, video_id, env):
|
|
|
- sql = f""" select * from crawler_video where platform in ("众妙音信", "刚刚都传", "吉祥幸福", "知青天天看", "zhufuquanzi", "祝福圈子", "haitunzhufu", "海豚祝福") and out_video_id="{video_id}"; """
|
|
|
+ sql = f""" select * from crawler_video where platform in ("众妙音信", "刚刚都传", "吉祥幸福", "知青天天看", "zhufuquanzi", "祝福圈子", "haitunzhufu", "海豚祝福", "小年糕") and out_video_id="{video_id}"; """
|
|
|
repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
|
|
|
return len(repeat_video)
|
|
|
|
|
|
@classmethod
|
|
|
def swipe_up(cls, driver: WebDriver):
|
|
|
- cls.search_elements(driver, '//*[@class="bless--list"]')
|
|
|
+ cls.search_elements(driver, '//*[@class="list-list--list"]')
|
|
|
size = driver.get_window_size()
|
|
|
driver.swipe(int(size["width"] * 0.5), int(size["height"] * 0.8),
|
|
|
int(size["width"] * 0.5), int(size["height"] * 0.4), 200)
|
|
|
|
|
|
@classmethod
|
|
|
- def get_video_url(cls, log_type, crawler, driver: WebDriver, video_title_element):
|
|
|
+ def get_video_url(cls, log_type, crawler, driver: WebDriver, video_title_element, env):
|
|
|
for i in range(3):
|
|
|
- cls.search_elements(driver, '//*[@class="bless--list"]')
|
|
|
+ cls.search_elements(driver, '//*[@class="list-list--list"]')
|
|
|
Common.logger(log_type, crawler).info(f"video_title_element:{video_title_element[0]}")
|
|
|
time.sleep(1)
|
|
|
Common.logger(log_type, crawler).info("滑动标题至可见状态")
|
|
|
- driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'});", video_title_element[0])
|
|
|
+ driver.execute_script("arguments[0].scrollIntoView({block:'center',inline:'center'});",
|
|
|
+ video_title_element[0])
|
|
|
time.sleep(3)
|
|
|
Common.logger(log_type, crawler).info("点击标题")
|
|
|
video_title_element[0].click()
|
|
|
- # driver.execute_script("arguments[0].click();", video_title_element[0])
|
|
|
+ cls.check_to_applet(log_type=log_type, crawler=crawler, driver=driver, env=env,
|
|
|
+ xpath=r'//wx-video[@class="dynamic-index--video-item dynamic-index--video"]')
|
|
|
Common.logger(log_type, crawler).info("点击标题完成")
|
|
|
- time.sleep(1)
|
|
|
- video_url_elements = cls.search_elements(driver, '//*[@class="index--video-item index--video"]')
|
|
|
+ time.sleep(10)
|
|
|
+ video_url_elements = cls.search_elements(driver,
|
|
|
+ '//wx-video[@class="dynamic-index--video-item dynamic-index--video"]')
|
|
|
if video_url_elements:
|
|
|
return video_url_elements[0].get_attribute("src")
|
|
|
|
|
@@ -163,31 +159,24 @@ class XiaoNianGaoPlusRecommend:
|
|
|
def get_videoList(cls, log_type, crawler, driver: WebDriver, env, rule_dict, our_uid):
|
|
|
mq = MQ(topic_name="topic_crawler_etl_" + env)
|
|
|
driver.implicitly_wait(20)
|
|
|
+ # 切换到 web_view
|
|
|
cls.check_to_applet(log_type=log_type, crawler=crawler, env=env, driver=driver,
|
|
|
- xpath='//*[@class="tags--tag tags--tag-0 tags--checked"]')
|
|
|
+ xpath='//*[@class="tab-bar--tab tab-bar--tab-selected"]')
|
|
|
time.sleep(1)
|
|
|
-
|
|
|
page = 0
|
|
|
while True:
|
|
|
- # if cls.search_elements(driver, '//*[@class="bless--list"]') is None:
|
|
|
- # Common.logger(log_type, crawler).info("窗口已销毁\n")
|
|
|
- # Common.logging(log_type, crawler, env, '窗口已销毁\n')
|
|
|
- # cls.i = 0
|
|
|
- # cls.download_cnt = 0
|
|
|
- # cls.element_list = []
|
|
|
- # return
|
|
|
- #
|
|
|
- # cls.swipe_up(driver)
|
|
|
-
|
|
|
+ if cls.search_elements(driver, '//*[@class="list-list--list"]') is None:
|
|
|
+ Common.logger(log_type, crawler).info("窗口已销毁\n")
|
|
|
+ Common.logging(log_type, crawler, env, '窗口已销毁\n')
|
|
|
+ cls.i = 0
|
|
|
+ cls.download_cnt = 0
|
|
|
+ cls.element_list = []
|
|
|
+ return
|
|
|
|
|
|
+ cls.swipe_up(driver)
|
|
|
page_source = driver.page_source
|
|
|
- # print(page_source)
|
|
|
- # return
|
|
|
soup = BeautifulSoup(page_source, 'html.parser')
|
|
|
soup.prettify()
|
|
|
- print(soup)
|
|
|
- return
|
|
|
-
|
|
|
video_list_elements = soup.findAll("wx-view", class_="expose--adapt-parent")
|
|
|
# video_list_elements 有,cls.element_list 中没有的元素
|
|
|
video_list_elements = list(set(video_list_elements).difference(set(cls.element_list)))
|
|
@@ -198,7 +187,7 @@ class XiaoNianGaoPlusRecommend:
|
|
|
|
|
|
if len(video_list_elements) == 0:
|
|
|
for i in range(10):
|
|
|
- Common.logger(log_type, crawler).info(f"向上滑动第{i+1}次")
|
|
|
+ Common.logger(log_type, crawler).info(f"向上滑动第{i + 1}次")
|
|
|
cls.swipe_up(driver)
|
|
|
time.sleep(0.5)
|
|
|
continue
|
|
@@ -215,16 +204,22 @@ class XiaoNianGaoPlusRecommend:
|
|
|
cls.i += 1
|
|
|
Common.logger(log_type, crawler).info(f"第{cls.i}条视频")
|
|
|
Common.logging(log_type, crawler, env, f"第{cls.i}条视频")
|
|
|
-
|
|
|
+ # 标题
|
|
|
video_title = video_element.find("wx-view", class_="dynamic--title").text
|
|
|
+ # 播放量字符串
|
|
|
play_str = video_element.find("wx-view", class_="dynamic--views").text
|
|
|
- like_str = video_element.findAll("wx-view", class_="dynamic--commerce-btn-text")[0].text
|
|
|
- comment_str = video_element.findAll("wx-view", class_="dynamic--commerce-btn-text")[1].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:
|
|
@@ -300,7 +295,7 @@ class XiaoNianGaoPlusRecommend:
|
|
|
continue
|
|
|
Common.logger(log_type, crawler).info("点击标题,进入视频详情页")
|
|
|
Common.logging(log_type, crawler, env, "点击标题,进入视频详情页")
|
|
|
- video_url = cls.get_video_url(log_type, crawler, driver, video_title_element)
|
|
|
+ video_url = cls.get_video_url(log_type, crawler, driver, video_title_element, env=env)
|
|
|
if video_url is None:
|
|
|
Common.logger(log_type, crawler).info("未获取到视频播放地址\n")
|
|
|
driver.press_keycode(AndroidKey.BACK)
|
|
@@ -316,6 +311,7 @@ class XiaoNianGaoPlusRecommend:
|
|
|
video_dict["user_id"] = our_uid
|
|
|
video_dict["publish_time"] = video_dict["publish_time_str"]
|
|
|
mq.send_msg(video_dict)
|
|
|
+ # print(video_dict)
|
|
|
cls.download_cnt += 1
|
|
|
driver.press_keycode(AndroidKey.BACK)
|
|
|
time.sleep(5)
|
|
@@ -332,7 +328,7 @@ class XiaoNianGaoPlusRecommend:
|
|
|
if __name__ == "__main__":
|
|
|
rule_dict1 = {"period": {"min": 365, "max": 365},
|
|
|
"duration": {"min": 30, "max": 1800},
|
|
|
- "favorite_cnt": {"min": 5000, "max": 0},
|
|
|
- "videos_cnt": {"min": 10, "max": 20},
|
|
|
- "share_cnt": {"min": 1000, "max": 0}}
|
|
|
+ "favorite_cnt": {"min": 0, "max": 0},
|
|
|
+ "videos_cnt": {"min": 100, "max": 0},
|
|
|
+ "share_cnt": {"min": 0, "max": 0}}
|
|
|
XiaoNianGaoPlusRecommend.start_wechat("recommend", "xiaoniangao", "dev", rule_dict1, 6267141)
|