|  | @@ -12,17 +12,12 @@
 | 
											
												
													
														|  |          3.2 python .\weixinzhishu\weixinzhishu_main\search_key.py
 |  |          3.2 python .\weixinzhishu\weixinzhishu_main\search_key.py
 | 
											
												
													
														|  |  每分钟获取最新search_key,写入飞书: https://w42nne6hzg.feishu.cn/sheets/shtcnqhMRUGunIfGnGXMOBYiy4K?sheet=sVL74k
 |  |  每分钟获取最新search_key,写入飞书: https://w42nne6hzg.feishu.cn/sheets/shtcnqhMRUGunIfGnGXMOBYiy4K?sheet=sVL74k
 | 
											
												
													
														|  |  """
 |  |  """
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |  import json
 |  |  import json
 | 
											
												
													
														|  |  import os
 |  |  import os
 | 
											
												
													
														|  | -import shutil
 |  | 
 | 
											
												
													
														|  |  import sys
 |  |  import sys
 | 
											
												
													
														|  |  import time
 |  |  import time
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |  import psutil
 |  |  import psutil
 | 
											
												
													
														|  |  from appium import webdriver
 |  |  from appium import webdriver
 | 
											
												
													
														|  | -from pyasn1.type.univ import Integer
 |  | 
 | 
											
												
													
														|  | -from selenium.common import NoSuchElementException, WebDriverException
 |  | 
 | 
											
												
													
														|  |  from selenium.webdriver.common.by import By
 |  |  from selenium.webdriver.common.by import By
 | 
											
												
													
														|  |  sys.path.append(os.getcwd())
 |  |  sys.path.append(os.getcwd())
 | 
											
												
													
														|  |  from common.common import Common
 |  |  from common.common import Common
 | 
											
										
											
												
													
														|  | @@ -33,30 +28,29 @@ class Searchkey:
 | 
											
												
													
														|  |      @classmethod
 |  |      @classmethod
 | 
											
												
													
														|  |      def start_wechat(cls, log_type, crawler):
 |  |      def start_wechat(cls, log_type, crawler):
 | 
											
												
													
														|  |          try:
 |  |          try:
 | 
											
												
													
														|  | -            # Common.logger(log_type, crawler).info('启动"微信"')
 |  | 
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).info('启动"微信"')
 | 
											
												
													
														|  |              desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
 |  |              desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
 | 
											
												
													
														|  |              driver = webdriver.Remote(
 |  |              driver = webdriver.Remote(
 | 
											
												
													
														|  |                  command_executor='http://127.0.0.1:4723',
 |  |                  command_executor='http://127.0.0.1:4723',
 | 
											
												
													
														|  |                  desired_capabilities=desired_caps)
 |  |                  desired_capabilities=desired_caps)
 | 
											
												
													
														|  |              driver.implicitly_wait(10)
 |  |              driver.implicitly_wait(10)
 | 
											
												
													
														|  | -            # Common.logger(log_type, crawler).info('点击微信指数')
 |  | 
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).info('选择对话人"微信同步助手"')
 | 
											
												
													
														|  | 
 |  | +            driver.find_elements(By.NAME, '微信同步助手')[0].click()
 | 
											
												
													
														|  | 
 |  | +            time.sleep(1)
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).info('点击"微信指数"')
 | 
											
												
													
														|  |              driver.find_elements(By.NAME, '消息')[-1].click()
 |  |              driver.find_elements(By.NAME, '消息')[-1].click()
 | 
											
												
													
														|  | -            time.sleep(2)
 |  | 
 | 
											
												
													
														|  | -            # cls.kill_pid(log_type, crawler)
 |  | 
 | 
											
												
													
														|  | -            # time.sleep(2)
 |  | 
 | 
											
												
													
														|  | 
 |  | +            time.sleep(1)
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).info('退出微信')
 | 
											
												
													
														|  |              driver.quit()
 |  |              driver.quit()
 | 
											
												
													
														|  | -            # time.sleep(2)
 |  | 
 | 
											
												
													
														|  | -            # windowHandles = driver.getWindowHandles()
 |  | 
 | 
											
												
													
														|  | -            print('关闭微信指数')
 |  | 
 | 
											
												
													
														|  | -            new_driver = cls.close_weixinzhishu()
 |  | 
 | 
											
												
													
														|  | -            new_driver.find_elements(By.NAME, '关闭')[-1].click()
 |  | 
 | 
											
												
													
														|  | -            # print(windowHandles)
 |  | 
 | 
											
												
													
														|  | -            # cls.rmtree_WMPFRuntime()
 |  | 
 | 
											
												
													
														|  | 
 |  | +            time.sleep(1)
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).info('关闭微信指数')
 | 
											
												
													
														|  | 
 |  | +            weixinzhishu_driver = cls.close_weixinzhishu(log_type, crawler)
 | 
											
												
													
														|  | 
 |  | +            weixinzhishu_driver.find_elements(By.NAME, '关闭')[-1].click()
 | 
											
												
													
														|  |          except Exception as e:
 |  |          except Exception as e:
 | 
											
												
													
														|  |              Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
 |  |              Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      @classmethod
 |  |      @classmethod
 | 
											
												
													
														|  | -    def close_weixinzhishu(cls, app_name='微信指数'):
 |  | 
 | 
											
												
													
														|  | 
 |  | +    def close_weixinzhishu(cls, log_type, crawler, app_name='微信指数'):
 | 
											
												
													
														|  |          """
 |  |          """
 | 
											
												
													
														|  |          *通过名字找到windowsdriver
 |  |          *通过名字找到windowsdriver
 | 
											
												
													
														|  |          *通过窗口名称,从桌面对象获取webdriver对象
 |  |          *通过窗口名称,从桌面对象获取webdriver对象
 | 
											
										
											
												
													
														|  | @@ -66,15 +60,13 @@ class Searchkey:
 | 
											
												
													
														|  |              new_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=new_caps)
 |  |              new_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=new_caps)
 | 
											
												
													
														|  |              windowElement = new_driver.find_elements(By.NAME, app_name)
 |  |              windowElement = new_driver.find_elements(By.NAME, app_name)
 | 
											
												
													
														|  |              if len(windowElement) != 0:
 |  |              if len(windowElement) != 0:
 | 
											
												
													
														|  | -                newWindowHandle = windowElement[0].get_attribute("NativeWindowHandle")
 |  | 
 | 
											
												
													
														|  | 
 |  | +                newWindowHandle = hex(int(windowElement[0].get_attribute("NativeWindowHandle")))
 | 
											
												
													
														|  |                  app_caps = {"appTopLevelWindow": newWindowHandle}
 |  |                  app_caps = {"appTopLevelWindow": newWindowHandle}
 | 
											
												
													
														|  |                  app_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
 |  |                  app_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
 | 
											
												
													
														|  |                                                desired_capabilities=app_caps)
 |  |                                                desired_capabilities=app_caps)
 | 
											
												
													
														|  |                  return app_driver
 |  |                  return app_driver
 | 
											
												
													
														|  | -        except NoSuchElementException:
 |  | 
 | 
											
												
													
														|  | -            print(f"没有找到对应的名字的窗口:{app_name}")
 |  | 
 | 
											
												
													
														|  | -        except WebDriverException:
 |  | 
 | 
											
												
													
														|  | -            print("连接winappdriver失败,请检查是否启动")
 |  | 
 | 
											
												
													
														|  | 
 |  | +        except Exception as e:
 | 
											
												
													
														|  | 
 |  | +            Common.logger(log_type, crawler).error(f"close_weixinzhishu异常:{e}\n")
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |      @classmethod
 |  |      @classmethod
 | 
											
												
													
														|  |      def kill_pid(cls, log_type, crawler):
 |  |      def kill_pid(cls, log_type, crawler):
 | 
											
										
											
												
													
														|  | @@ -98,13 +90,6 @@ class Searchkey:
 | 
											
												
													
														|  |          except Exception as e:
 |  |          except Exception as e:
 | 
											
												
													
														|  |              Common.logger(log_type, crawler).error(f'kill_pid异常:{e}\n')
 |  |              Common.logger(log_type, crawler).error(f'kill_pid异常:{e}\n')
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -    @classmethod
 |  | 
 | 
											
												
													
														|  | -    def rmtree_WMPFRuntime(cls):
 |  | 
 | 
											
												
													
														|  | -        WMPFRuntime_path = r"C:\Users\guosh\AppData\Roaming\Tencent\WeChat\XPlugin\Plugins\WMPFRuntime"
 |  | 
 | 
											
												
													
														|  | -        shutil.rmtree(WMPFRuntime_path)
 |  | 
 | 
											
												
													
														|  | -        if not os.path.exists(WMPFRuntime_path):
 |  | 
 | 
											
												
													
														|  | -            os.mkdir(WMPFRuntime_path)
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  |      @classmethod
 |  |      @classmethod
 | 
											
												
													
														|  |      def get_search_key(cls, log_type, crawler):
 |  |      def get_search_key(cls, log_type, crawler):
 | 
											
												
													
														|  |          try:
 |  |          try:
 | 
											
										
											
												
													
														|  | @@ -194,8 +179,9 @@ class Searchkey:
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  if __name__ == '__main__':
 |  |  if __name__ == '__main__':
 | 
											
												
													
														|  | -    Searchkey.start_wechat('searchkey', 'weixinzhishu')
 |  | 
 | 
											
												
													
														|  | -    # while True:
 |  | 
 | 
											
												
													
														|  | -    #     Searchkey.write_search_key_to_feishu('searchkey', 'weixinzhishu')
 |  | 
 | 
											
												
													
														|  | -    #     Common.logger('searchkey', 'weixinzhishu').info('休眠 1 分钟')
 |  | 
 | 
											
												
													
														|  | -    #     time.sleep(60)
 |  | 
 | 
											
												
													
														|  | 
 |  | +    while True:
 | 
											
												
													
														|  | 
 |  | +        Searchkey.write_search_key_to_feishu('searchkey', 'weixinzhishu')
 | 
											
												
													
														|  | 
 |  | +        Common.logger('searchkey', 'weixinzhishu').info('休眠 1 分钟')
 | 
											
												
													
														|  | 
 |  | +        time.sleep(60)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    # Searchkey.start_wechat('searchkey', 'weixinzhishu')
 |