get_search_key.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2023/2/10
  4. import json
  5. import os
  6. import sys
  7. import time
  8. import psutil as psutil
  9. from appium import webdriver
  10. from selenium.webdriver.common.by import By
  11. sys.path.append(os.getcwd())
  12. from common.common import Common
  13. class Searchkey:
  14. @classmethod
  15. def start_wechat(cls, log_type, crawler):
  16. try:
  17. # Common.logger(log_type, crawler).info('启动"微信"')
  18. print('启动"微信"')
  19. desired_caps = {'app': r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}
  20. driver = webdriver.Remote(
  21. command_executor='http://127.0.0.1:4723',
  22. desired_capabilities=desired_caps)
  23. driver.implicitly_wait(10)
  24. # Common.logger(log_type).info('点击"聊天窗口"')
  25. # driver.find_element(By.NAME, '聊天').click()
  26. #
  27. # Common.logger(log_type).info('点击"爬虫群"')
  28. # driver.find_elements(By.NAME, '爬虫群')[0].click()
  29. # Common.logger(log_type, crawler).info('点击微信指数')
  30. print('点击微信指数')
  31. driver.find_elements(By.NAME, '消息')[-1].click()
  32. # Common.logger(log_type, crawler).info('休眠 10 秒,退出微信指数')
  33. # time.sleep(10)
  34. # cls.kill_pid(log_type)
  35. print("3秒后退出微信")
  36. time.sleep(3)
  37. # Common.logger(log_type, crawler).info('退出微信')
  38. driver.quit()
  39. except Exception as e:
  40. Common.logger(log_type, crawler).error('click_video异常:{}', e)
  41. @classmethod
  42. def get_url(cls, log_type, crawler):
  43. try:
  44. # charles 抓包文件保存目录
  45. charles_file_dir = r"./weixinzhishu_chlsfiles/"
  46. if len(os.listdir(charles_file_dir)) == 0:
  47. Common.logger(log_type).info("未找到chlsfile文件,等待2s")
  48. time.sleep(2)
  49. else:
  50. # 目标文件夹下所有文件
  51. all_file = sorted(os.listdir(charles_file_dir))
  52. # 获取到目标文件
  53. old_file = all_file[-1]
  54. # 分离文件名与扩展名
  55. new_file = os.path.splitext(old_file)
  56. # 重命名文件后缀
  57. os.rename(os.path.join(charles_file_dir, old_file),
  58. os.path.join(charles_file_dir, new_file[0] + ".txt"))
  59. with open(charles_file_dir + new_file[0] + ".txt", encoding='utf-8-sig', errors='ignore') as f:
  60. contents = json.load(f, strict=False)
  61. video_url_list = []
  62. cover_url_list = []
  63. if "finder.video.qq.com" in [text['host'] for text in contents]:
  64. for text in contents:
  65. if text["host"] == "finder.video.qq.com" and text["path"] == "/251/20302/stodownload":
  66. video_url_list.append(text)
  67. elif text["host"] == "finder.video.qq.com" and text["path"] == "/251/20304/stodownload":
  68. cover_url_list.append(text)
  69. video_url = video_url_list[0]['host']+video_url_list[0]['path']+'?'+video_url_list[0]['query']
  70. cover_url = cover_url_list[0]['host']+cover_url_list[0]['path']+'?'+cover_url_list[0]['query']
  71. head_url = cover_url
  72. # print(f'video_url:{video_url}')
  73. # print(f'cover_url:{cover_url}')
  74. # print(f'head_url:{head_url}')
  75. return video_url, cover_url, head_url
  76. else:
  77. Common.logger(log_type).info("未找到url")
  78. return '未找到url'
  79. except Exception as e:
  80. Common.logger(log_type).exception("get_url异常:{}\n", e)
  81. return None
  82. if __name__ == '__main__':
  83. Searchkey.start_wechat('weixin', 'weixinzhishu')
  84. # while True:
  85. # ShipinhaoWindows.run_get_url('recommend')
  86. # Common.del_logs('recommend')
  87. # time.sleep(1)
  88. pass