|  | @@ -12,17 +12,12 @@
 | 
	
		
			
				|  |  |          3.2 python .\weixinzhishu\weixinzhishu_main\search_key.py
 | 
	
		
			
				|  |  |  每分钟获取最新search_key,写入飞书: https://w42nne6hzg.feishu.cn/sheets/shtcnqhMRUGunIfGnGXMOBYiy4K?sheet=sVL74k
 | 
	
		
			
				|  |  |  """
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  import json
 | 
	
		
			
				|  |  |  import os
 | 
	
		
			
				|  |  | -import shutil
 | 
	
		
			
				|  |  |  import sys
 | 
	
		
			
				|  |  |  import time
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  import psutil
 | 
	
		
			
				|  |  |  from appium import webdriver
 | 
	
		
			
				|  |  | -from pyasn1.type.univ import Integer
 | 
	
		
			
				|  |  | -from selenium.common import NoSuchElementException, WebDriverException
 | 
	
		
			
				|  |  |  from selenium.webdriver.common.by import By
 | 
	
		
			
				|  |  |  sys.path.append(os.getcwd())
 | 
	
		
			
				|  |  |  from common.common import Common
 | 
	
	
		
			
				|  | @@ -33,30 +28,29 @@ class Searchkey:
 | 
	
		
			
				|  |  |      @classmethod
 | 
	
		
			
				|  |  |      def start_wechat(cls, log_type, crawler):
 | 
	
		
			
				|  |  |          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"}
 | 
	
		
			
				|  |  |              driver = webdriver.Remote(
 | 
	
		
			
				|  |  |                  command_executor='http://127.0.0.1:4723',
 | 
	
		
			
				|  |  |                  desired_capabilities=desired_caps)
 | 
	
		
			
				|  |  |              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()
 | 
	
		
			
				|  |  | -            time.sleep(2)
 | 
	
		
			
				|  |  | -            # cls.kill_pid(log_type, crawler)
 | 
	
		
			
				|  |  | -            # time.sleep(2)
 | 
	
		
			
				|  |  | +            time.sleep(1)
 | 
	
		
			
				|  |  | +            Common.logger(log_type, crawler).info('退出微信')
 | 
	
		
			
				|  |  |              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:
 | 
	
		
			
				|  |  |              Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @classmethod
 | 
	
		
			
				|  |  | -    def close_weixinzhishu(cls, app_name='微信指数'):
 | 
	
		
			
				|  |  | +    def close_weixinzhishu(cls, log_type, crawler, app_name='微信指数'):
 | 
	
		
			
				|  |  |          """
 | 
	
		
			
				|  |  |          *通过名字找到windowsdriver
 | 
	
		
			
				|  |  |          *通过窗口名称,从桌面对象获取webdriver对象
 | 
	
	
		
			
				|  | @@ -66,15 +60,13 @@ class Searchkey:
 | 
	
		
			
				|  |  |              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)
 | 
	
		
			
				|  |  |              if len(windowElement) != 0:
 | 
	
		
			
				|  |  | -                newWindowHandle = windowElement[0].get_attribute("NativeWindowHandle")
 | 
	
		
			
				|  |  | +                newWindowHandle = hex(int(windowElement[0].get_attribute("NativeWindowHandle")))
 | 
	
		
			
				|  |  |                  app_caps = {"appTopLevelWindow": newWindowHandle}
 | 
	
		
			
				|  |  |                  app_driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
 | 
	
		
			
				|  |  |                                                desired_capabilities=app_caps)
 | 
	
		
			
				|  |  |                  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
 | 
	
		
			
				|  |  |      def kill_pid(cls, log_type, crawler):
 | 
	
	
		
			
				|  | @@ -98,13 +90,6 @@ class Searchkey:
 | 
	
		
			
				|  |  |          except Exception as e:
 | 
	
		
			
				|  |  |              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
 | 
	
		
			
				|  |  |      def get_search_key(cls, log_type, crawler):
 | 
	
		
			
				|  |  |          try:
 | 
	
	
		
			
				|  | @@ -194,8 +179,9 @@ class Searchkey:
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  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')
 |