search_key.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/2/10
  4. """
  5. 获取微信指数小程序请求参数:search_key
  6. 1. 启动 WinAppDriver.exe
  7. 2. 启动 Charles.exe:
  8. 2.1 选中 Proxy - Windows Proxy
  9. 2.2 选中 Tools - Auto Save - Enable Auto Save
  10. 3. 启动 Python 脚本:
  11. 3.1 cd D:\piaoquan_crawler
  12. 3.2 python .\weixinzhishu\weixinzhishu_main\search_key.py
  13. 每分钟获取最新search_key,写入飞书: https://w42nne6hzg.feishu.cn/sheets/shtcnqhMRUGunIfGnGXMOBYiy4K?sheet=sVL74k
  14. """
  15. import json
  16. import os
  17. import sys
  18. import time
  19. from appium import webdriver
  20. from selenium.webdriver.common.by import By
  21. sys.path.append(os.getcwd())
  22. from common.common import Common
  23. from common.feishu import Feishu
  24. class Searchkey:
  25. @classmethod
  26. def start_wechat(cls, log_type, crawler):
  27. try:
  28. # Common.logger(log_type, crawler).info('启动"微信"')
  29. desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
  30. driver = webdriver.Remote(
  31. command_executor='http://127.0.0.1:4723',
  32. desired_capabilities=desired_caps)
  33. driver.implicitly_wait(10)
  34. # Common.logger(log_type, crawler).info('点击微信指数')
  35. driver.find_elements(By.NAME, '消息')[-1].click()
  36. # Common.logger(log_type, crawler).info('休眠 3 秒,退出微信')
  37. time.sleep(3)
  38. driver.quit()
  39. except Exception as e:
  40. Common.logger(log_type, crawler).error(f'start_wechat异常:{e}\n')
  41. @classmethod
  42. def get_search_key(cls, log_type, crawler):
  43. try:
  44. # charles 抓包文件保存目录
  45. chlsfile_path = f"./{crawler}/{crawler}_chlsfiles/"
  46. if len(os.listdir(chlsfile_path)) == 0:
  47. Common.logger(log_type, crawler).info("chlsfile文件夹为空,等待10s")
  48. cls.start_wechat(log_type, crawler)
  49. time.sleep(10)
  50. cls.get_search_key(log_type, crawler)
  51. else:
  52. Common.logger(log_type, crawler).info(f"chlsfile_list:{sorted(os.listdir(chlsfile_path))}")
  53. # 获取最新的 chlsfile
  54. chlsfile = sorted(os.listdir(chlsfile_path))[-1]
  55. # 分离文件名与扩展名
  56. new_file = os.path.splitext(chlsfile)
  57. # 重命名文件后缀
  58. os.rename(os.path.join(chlsfile_path, chlsfile),
  59. os.path.join(chlsfile_path, new_file[0] + ".txt"))
  60. with open(f"{chlsfile_path}{new_file[0]}.txt", encoding='utf-8-sig', errors='ignore') as f:
  61. contents = json.load(f, strict=False)
  62. if "search.weixin.qq.com" not in [text['host'] for text in contents]:
  63. return "未找到search_key"
  64. else:
  65. for content in contents:
  66. if content["host"] == "search.weixin.qq.com" and content["path"] == "/cgi-bin/wxaweb/wxindexgetusergroup":
  67. text = content['request']['body']['text']
  68. search_key = json.loads(text)['search_key']
  69. return search_key
  70. except Exception as e:
  71. Common.logger(log_type, crawler).exception(f"get_search_key异常:{e}\n")
  72. return None
  73. @classmethod
  74. def remove_file(cls, log_type, crawler):
  75. try:
  76. all_file_path = f"./{crawler}/{crawler}_chlsfiles/"
  77. if not os.path.exists(all_file_path):
  78. os.mkdir(all_file_path)
  79. all_file = os.listdir(f"./{crawler}/{crawler}_chlsfiles/")
  80. for file in all_file:
  81. os.remove(f"./{crawler}/{crawler}_chlsfiles/{file}")
  82. except Exception as e:
  83. Common.logger(log_type, crawler).error(f"remove_file异常:{e}\n")
  84. @classmethod
  85. def del_search_key_from_feishu(cls, log_type, crawler):
  86. try:
  87. sheet = Feishu.get_values_batch(log_type, crawler, 'sVL74k')
  88. if len(sheet) <= 21:
  89. # print('<=20行')
  90. return
  91. else:
  92. Feishu.dimension_range(log_type, crawler, 'sVL74k', 'ROWS', 22, 22)
  93. cls.del_search_key_from_feishu(log_type, crawler)
  94. except Exception as e:
  95. Common.logger(log_type, crawler).error(f"del_search_key_from_feishu异常:{e}\n")
  96. @classmethod
  97. def write_search_key_to_feishu(cls, log_type, crawler):
  98. Common.logger(log_type, crawler).info('清除 chlsfiles 文件夹')
  99. cls.remove_file(log_type, crawler)
  100. Common.logger(log_type, crawler).info('启动微信指数小程序')
  101. cls.start_wechat(log_type, crawler)
  102. Common.logger(log_type, crawler).info('获取 search_key')
  103. while True:
  104. search_key = cls.get_search_key(log_type, crawler)
  105. if search_key is None or search_key == "未找到search_key":
  106. time.sleep(3)
  107. Common.logger(log_type, crawler).info('未找到search_key,重启打开微信指数,获取 search_key')
  108. cls.start_wechat(log_type, crawler)
  109. cls.get_search_key(log_type, crawler)
  110. else:
  111. Common.logger(log_type, crawler).info(f'已获取 search_key:{search_key}')
  112. Feishu.insert_columns(log_type, crawler, 'sVL74k', 'ROWS', 1, 2)
  113. time.sleep(1)
  114. time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))
  115. Feishu.update_values(log_type, crawler, 'sVL74k', 'A2:B2', [[time_str, search_key]])
  116. cls.del_search_key_from_feishu(log_type, crawler)
  117. Common.logger(log_type, crawler).info(f"search_key:{search_key}写入飞书表成功\n")
  118. return
  119. if __name__ == '__main__':
  120. while True:
  121. Searchkey.write_search_key_to_feishu('searchkey', 'weixinzhishu')
  122. Common.logger('searchkey', 'weixinzhishu').info('休眠 1 分钟')
  123. time.sleep(60)