test_sph_rec.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/7/26
  4. import datetime
  5. import json
  6. import os
  7. import sys
  8. import time
  9. from datetime import date, timedelta
  10. from hashlib import md5
  11. from appium import webdriver
  12. from appium.webdriver.extensions.android.nativekey import AndroidKey
  13. from appium.webdriver.webdriver import WebDriver
  14. from selenium.common import NoSuchElementException
  15. from selenium.webdriver.common.by import By
  16. sys.path.append(os.getcwd())
  17. def start_wechat():
  18. chromedriverExecutable = r'/opt/homebrew/bin/chromedriver'
  19. caps = {
  20. "platformName": "Android", # 手机操作系统 Android / iOS
  21. "deviceName": "Android", # 连接的设备名(模拟器或真机),安卓可以随便写
  22. "platforVersion": "13", # 手机对应的系统版本(Android 13)
  23. "appPackage": "com.tencent.mm", # 被测APP的包名,乐活圈 Android
  24. "appActivity": ".ui.LauncherUI", # 启动的Activity名
  25. "autoGrantPermissions": True, # 让 appium 自动授权 base 权限,
  26. # 如果 noReset 为 True,则该条不生效(该参数为 Android 独有),对应的值为 True 或 False
  27. "unicodekeyboard": True, # 使用自带输入法,输入中文时填True
  28. "resetkeyboard": True, # 执行完程序恢复原来输入法
  29. "noReset": True, # 不重置APP
  30. "recreateChromeDriverSessions": True, # 切换到非 chrome-Driver 会 kill 掉 session,就不需要手动 kill 了
  31. "printPageSourceOnFailure": True, # 找不到元素时,appium log 会完整记录当前页面的 pagesource
  32. "newCommandTimeout": 6000, # 初始等待时间
  33. "automationName": "UiAutomator2", # 使用引擎,默认为 Appium,
  34. # 其中 Appium、UiAutomator2、Selendroid、Espresso 用于 Android,XCUITest 用于 iOS
  35. "showChromedriverLog": True,
  36. # "chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"},
  37. "chromeOptions": {"androidProcess": "com.tencent.mm:tools"},
  38. # "chromeOptions": {"androidProcess": "com.tencent.mm:toolsmp"},
  39. # "chromeOptions": {"androidProcess": "com.tencent.mm"},
  40. "enableWebviewDetailsCollection": True,
  41. "setWebContentsDebuggingEnabled": True,
  42. "chromedriverExecutable": chromedriverExecutable,
  43. }
  44. driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
  45. driver.implicitly_wait(10)
  46. tabs = driver.find_elements(By.ID, "com.tencent.mm:id/f2s")
  47. for tab in tabs:
  48. if tab.text == "发现":
  49. tab.click()
  50. time.sleep(0.5)
  51. break
  52. tabs = driver.find_elements(By.ID, "com.tencent.mm:id/f2s")
  53. for tab in tabs:
  54. if tab.text == "发现":
  55. tab.click()
  56. time.sleep(0.5)
  57. break
  58. textviews = driver.find_elements(By.ID, "android:id/title")
  59. for textview in textviews:
  60. if textview.text == "视频号":
  61. textview.click()
  62. time.sleep(0.5)
  63. break
  64. try:
  65. driver.find_element(By.ID, "com.tencent.mm:id/lqz").click()
  66. except NoSuchElementException:
  67. pass
  68. for i in range(100):
  69. try:
  70. if len(driver.find_elements(By.ID, "com.tencent.mm:id/dkf")) != 0:
  71. driver.swipe(10, 1600, 10, 300, 200)
  72. continue
  73. # video_dict = cls.get_video_info(driver)
  74. # for k, v in video_dict.items():
  75. # Common.logger(log_type, crawler).info(f"{k}:{v}")
  76. # Common.logging(log_type, crawler, env, f"video_dict:{video_dict}")
  77. #
  78. # if video_dict["video_title"] is None:
  79. # Common.logger(log_type, crawler).info("无效视频")
  80. # Common.logging(log_type, crawler, env, "无效视频")
  81. # elif (
  82. # download_rule(
  83. # log_type=log_type,
  84. # crawler=crawler,
  85. # video_dict=video_dict,
  86. # rule_dict=rule_dict,
  87. # )
  88. # is False
  89. # ):
  90. # Common.logger(log_type, crawler).info("不满足抓取规则")
  91. # Common.logging(log_type, crawler, env, "不满足抓取规则\n")
  92. # elif (
  93. # cls.repeat_out_video_id(
  94. # log_type, crawler, video_dict["video_id"], env
  95. # )
  96. # != 0
  97. # ):
  98. # Common.logger(log_type, crawler).info("视频已下载")
  99. # Common.logging(log_type, crawler, env, "视频已下载\n")
  100. # else:
  101. # cls.download_video_list.append(video_dict)
  102. # if i + 1 == scan_count:
  103. # Common.logger(log_type, crawler).info("扫描一轮结束\n")
  104. # Common.logging(log_type, crawler, env, "扫描一轮结束\n")
  105. # return
  106. # Common.logger(log_type, crawler).info(
  107. # f"已抓取符合规则视频{len(cls.download_video_list)}条,滑动至下一个视频\n"
  108. # )
  109. # Common.logging(
  110. # log_type,
  111. # crawler,
  112. # env,
  113. # f"已抓取符合规则视频{len(cls.download_video_list)}条,滑动至下一个视频\n",
  114. # )
  115. # driver.swipe(10, 1600, 10, 300, 200)
  116. except Exception as e:
  117. print(123)
  118. # Common.logger(log_type, crawler).info(f"扫描单条视频时异常:{e}\n")
  119. # Common.logging(log_type, crawler, env, f"扫描单条视频时异常:{e}\n")
  120. time.sleep(50)
  121. driver.quit()
  122. # cls.search_video(
  123. # log_type=log_type,
  124. # crawler=crawler,
  125. # rule_dict=rule_dict,
  126. # user_dict=user_dict,
  127. # driver=driver,
  128. # env=env,
  129. # )
  130. # cls.close_wechat(log_type=log_type, crawler=crawler, env=env, driver=driver)
  131. def get_recommend_list(cls, log_type, crawler, rule_dict, scan_count, env):
  132. try:
  133. driver = cls.start_wechat(log_type, crawler, env)
  134. cls.get_videoList(
  135. log_type=log_type,
  136. crawler=crawler,
  137. rule_dict=rule_dict,
  138. env=env,
  139. scan_count=scan_count,
  140. driver=driver,
  141. )
  142. driver.quit()
  143. print("退出微信成功")
  144. except Exception as e:
  145. print("扫描异常", e)
  146. def main():
  147. while True:
  148. scan_count = 20
  149. rule_dict = {
  150. "period": {"min": 365, "max": 365},
  151. "duration": {"min": 10, "max": 1800},
  152. "favorite_cnt": {"min": 5000, "max": 0},
  153. "share_cnt": {"min": 1000, "max": 0},
  154. }
  155. if __name__ == "__main__":
  156. can_count = 20
  157. rule_dict = {
  158. "period": {"min": 365, "max": 365},
  159. "duration": {"min": 10, "max": 1800},
  160. "favorite_cnt": {"min": 5000, "max": 0},
  161. "share_cnt": {"min": 1000, "max": 0},
  162. }
  163. start_wechat()