yongyi_pig_manual.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # coding:utf-8
  2. import os
  3. import shutil
  4. import subprocess
  5. from time import sleep
  6. from bottle import route, run
  7. import datetime
  8. import hug
  9. import rarfile as rarfile
  10. from selenium import webdriver
  11. # 设置Chrome浏览器选项
  12. from selenium.common import exceptions
  13. from selenium.webdriver.chrome.service import Service
  14. from selenium.webdriver.common.by import By
  15. from selenium.webdriver.support import expected_conditions as EC
  16. from selenium.webdriver.support.wait import WebDriverWait
  17. chrome_driver_path = r"D:/eta/chromedriver-win64/chromedriver.exe"
  18. chrome_address = r"127.0.0.1:9222"
  19. save_to_dir = r'D:/eta/yongyi_data/download'
  20. new_dir = r'D:\eta\yongyi_data\yongyi\unread'
  21. rarfile.UNRAR_TOOL = r"D:\Program Files\WinRAR\UnRAR.exe"
  22. def rename_week_file(new_dir, rar_name):
  23. files = os.listdir(rar_name)
  24. for dir in files:
  25. file_path = os.path.join(rar_name, dir)
  26. if os.path.isdir(file_path):
  27. for f in os.listdir(file_path):
  28. print("f.title()" + f.title())
  29. if f.title().find("周度数据") != -1:
  30. file_name = os.path.basename(f.title())
  31. new_name = os.path.join(new_dir, file_name)
  32. file_name = file_name.lower()
  33. old_name = os.path.join(file_path, f)
  34. # os.rename(old_name, new_name)
  35. shutil.move(old_name, new_name)
  36. print(f'周度数据文件重命名成功, 旧文件名{old_name} 新文件名{new_name}')
  37. elif f.title().find("周度图表版图") != -1:
  38. file_name = os.path.basename(f.title())
  39. file_name = file_name.lower()
  40. new_name = os.path.join(new_dir, file_name)
  41. old_name = os.path.join(file_path, f)
  42. # os.rename(old_name, new_name)
  43. shutil.move(old_name, new_name)
  44. print(f'周度图表版图文件重命名成功, 旧文件名{old_name} 新文件名{new_name}')
  45. else:
  46. print(dir.title() + "不是一个文件夹")
  47. return
  48. return
  49. def get_yongyi_driver():
  50. # python+selunium定位已打开的浏览器
  51. # 创建一个 Chrome WebDriver 实例
  52. options = webdriver.ChromeOptions()
  53. # options.add_argument("headless")
  54. # options.add_argument('--headless')
  55. options.add_argument('--disable-gpu')
  56. options.add_argument('--no-sandbox')
  57. # 谷歌浏览器运行的默认调试端口:先用以下命令启动浏览器
  58. # 找到谷歌浏览器的程序地址,开启一个新的端口,并设置一个文件夹来保存浏览器的数据
  59. # /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --user-data-dir=/Users/xiexiaoyuan/data/selenium/automationProfile
  60. # --remote-debugging-port值,可以指定任何打开的端口
  61. # --user-data-dir标记,指定创建新Chrome配置文件的目录。它是为了确保在单独的配置文件中启动chrome,不会污染你的默认配置文件。
  62. #
  63. # 此时会打开一个浏览器页面,我们输入目标网址,输入账号密码,登录成功。
  64. # 登录之后,以后都不需要登录,它会把你这次登录的信息记入到 --user-data-dir指定的目录下
  65. # 后面你只需要python + selenium + webdriver定位到这个已经登录的浏览器进行操作就可以啦
  66. options.add_experimental_option("debuggerAddress", chrome_address)
  67. print("重新打开")
  68. options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, '
  69. 'like Gecko) Chrome/118.0.5993.70 Safari/537.36')
  70. options.add_argument(" window-size=1920,1080")
  71. # s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_114/chromedriver')
  72. s = Service(executable_path=chrome_driver_path)
  73. driver = webdriver.Chrome(service=s, options=options)
  74. driver.get('https://data.yongyizixun888.com/')
  75. return driver
  76. @hug.get('/yongyi/download')
  77. def yongyi_download(dayFlag, weekFlag):
  78. print("dayFlag:"+dayFlag)
  79. print("weekFlag:"+weekFlag)
  80. # 修改下载地址
  81. # save_to_dir = '/Users/xiexiaoyuan/Downloads'
  82. # save_to_dir = r'D:/eta/yongyi_data/download'
  83. # 获取当前时间,并将其格式化为指定的形式
  84. # new_dir = '/Users/xiexiaoyuan/Downloads/yongyi/unread'
  85. # new_dir = r'D:\eta\yongyi_data\yongyi\unread'
  86. try:
  87. try:
  88. driver = get_yongyi_driver()
  89. except Exception as e:
  90. print("Exception:")
  91. print(str(e))
  92. if str(e).find("chrome not reachable") != -1:
  93. print("浏览器未开启")
  94. return False
  95. else:
  96. return False
  97. sleep(3)
  98. account = driver.find_element(By.XPATH, '//*[@id="dr_member_info"]/a[1]').text
  99. print(account)
  100. # 下载涌溢日度数据库
  101. if dayFlag == '1':
  102. sleep(1)
  103. a = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[2]/a')
  104. print(a.get_attribute("href"))
  105. a.click()
  106. sleep(30)
  107. # 下载涌溢完整数据库
  108. if weekFlag == '1':
  109. sleep(2)
  110. b = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div[2]/div[3]/a')
  111. print(b.get_attribute("href"))
  112. b.click()
  113. sleep(30)
  114. # 查找文件并重命名
  115. os.chdir(save_to_dir)
  116. files = filter(os.path.isfile, os.listdir(save_to_dir))
  117. files = [os.path.join(save_to_dir, f) for f in files] # add path to each file
  118. files.sort(key=lambda x: os.path.getmtime(x), reverse=True)
  119. day_file = ""
  120. week_file = ""
  121. i =0
  122. for file in files:
  123. if i > 3:
  124. break
  125. print(file.title())
  126. if file.title().find("涌益咨询日度数据") != -1:
  127. if day_file == "":
  128. day_file = file
  129. if file.title().find("涌益咨询周度数据") != -1:
  130. if week_file == "":
  131. week_file = file
  132. if day_file != "" and week_file != "":
  133. break
  134. i = i+1
  135. if dayFlag == '1':
  136. if day_file != "":
  137. print(day_file.title())
  138. file_name = os.path.basename(day_file.title())
  139. file_name = file_name.lower()
  140. new_name = os.path.join(new_dir,file_name)
  141. print(f'新文件名{new_name}')
  142. print(f'旧文件名{day_file.title()}')
  143. # os.rename(day_file.title(), new_name)
  144. shutil.move(day_file.title(), new_name)
  145. print(f'日度文件重命名成功, 旧文件名{day_file.title()} 新文件名{new_name}')
  146. else:
  147. print("未找到日度下载文件")
  148. if weekFlag == '1':
  149. if week_file != "":
  150. print(week_file.title())
  151. filename = week_file.title()
  152. index = filename.find(".Rar")
  153. rar_name = filename[:index]
  154. # 解压缩
  155. rar_file = rarfile.RarFile(filename, 'r')
  156. rar_file.extractall(rar_name)
  157. rar_file.close()
  158. rename_week_file(new_dir, rar_name)
  159. else:
  160. print("未找到周度下载文件")
  161. return True
  162. except Exception as e:
  163. print("Exception:")
  164. print(str(e))
  165. return False
  166. if __name__ == "__main__":
  167. app = __hug__.http.server()
  168. run(app=app, reloader=True, host='127.0.0.1', port=7010)