yongyi_pig_manual.py 6.6 KB

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