metting.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # coding:utf-8
  2. from time import sleep
  3. import datetime
  4. import requests
  5. import openpyxl
  6. from selenium import webdriver
  7. # 设置Chrome浏览器选项
  8. from selenium.common import exceptions
  9. from selenium.webdriver.chrome.service import Service
  10. from selenium.webdriver.common.by import By
  11. """
  12. 根据table的id属性和table中的某一个元素定位其在table中的位置
  13. table包括表头,位置坐标都是从1开始算
  14. tableId:table的id属性
  15. queryContent:需要确定位置的内容
  16. """
  17. def get_table_content(driver, tableId, queryContent):
  18. # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
  19. table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr")
  20. table_list = [] # 存放table数据
  21. for tr in table_tr_list: # 遍历每一个tr
  22. # 将每一个tr的数据根据td查询出来,返回结果为list对象
  23. table_td_list = tr.find_elements(By.TAG_NAME, "td")
  24. row_list = []
  25. print(table_td_list)
  26. for td in table_td_list: # 遍历每一个td
  27. row_list.append(td.text) # 取出表格的数据,并放入行列表里
  28. table_list.append(row_list)
  29. # 循环遍历table数据,确定查询数据的位置
  30. # for i in range(len(table_list)):
  31. # for j in range(len(table_list[i])):
  32. # if queryContent == table_list[i][j]:
  33. # print("%r坐标为(%r,%r)" % (queryContent, i + 1, j + 1))
  34. # 写入文件
  35. def write_excel_xlsx(path, sheet_name, value):
  36. index = len(value) # 列表中所含元组的个数,从而确定写入Excel的行数
  37. # 打开Excel
  38. wb = openpyxl.Workbook()
  39. # wb = load_workbook(path)
  40. sheet = wb.active # 获得一个的工作表
  41. sheet.title = sheet_name
  42. # 设置格式
  43. sheet.column_dimensions['B'].width = 115
  44. # 按行加入
  45. for i in range(index):
  46. sheet.append(value[i])
  47. # 保存文件
  48. print(sheet.values)
  49. wb.save(path)
  50. print("题目写入数据成功!")
  51. def send_file(url, file_path):
  52. with open(file_path, 'rb') as file:
  53. files = {'file': file}
  54. response2 = requests.post(url, files=files)
  55. return response2
  56. if __name__ == "__main__":
  57. # 创建一个 Chrome WebDriver 实例
  58. options = webdriver.ChromeOptions()
  59. # options.add_argument("headless")
  60. options.add_argument('--headless')
  61. options.add_argument('--disable-gpu')
  62. options.add_argument('--no-sandbox')
  63. # options.add_argument(" window-size=1920,1080")
  64. s = Service(executable_path='/home/code/python/meeting_probabilities/chromedriver')
  65. # s = Service(executable_path='/Users/xi/Desktop/chromedriver')
  66. driver = webdriver.Chrome(service=s, options=options)
  67. # driver.maximize_window()
  68. driver.get(
  69. 'https://www.cmegroup.com/markets/interest-rates/cme-fedwatch-tool.html?redirect=/trading/interest-rates/countdown-to-fomc.html')
  70. sleep(2)
  71. try:
  72. driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div/section/span').click()
  73. driver.find_element(By.XPATH, '//*[@id="onetrust-accept-btn-handler"]').click()
  74. except exceptions.NoSuchElementException:
  75. print("弹框不存在")
  76. # page_height = driver.execute_script('return document.documentElement.scrollHeight') # 页面高度
  77. driver.execute_script("window.scrollBy(0,{})".format(600))
  78. driver.switch_to.frame("cmeIframe-jtxelq2f")
  79. sleep(2)
  80. # button = driver.find_element(By.XPATH, '//*[@id="ctl00_MainContent_ucViewControl_IntegratedFedWatchTool_lbPTree"]')
  81. driver.execute_script(
  82. "javascript:__doPostBack('ctl00$MainContent$ucViewControl_IntegratedFedWatchTool$lbPTree','')")
  83. sleep(2)
  84. table = driver.find_element(By.XPATH, '//*[@id="MainContent_pnlContainer"]/div[3]/div/div/table[2]')
  85. table.screenshot(r'meeting.png')
  86. print(table.text)
  87. # 按行查询表格的数据,取出的数据是一整行,按空格分隔每一列的数据
  88. table_tr_list = table.find_elements(By.TAG_NAME, "tr")
  89. table_list = [] # 存放table数据
  90. th_flag = False
  91. title = 'MEETING PROBABILITIES'
  92. i = 0
  93. for tr in table_tr_list: # 遍历每一个tr
  94. # 将每一个tr的数据根据td查询出来,返回结果为list对象
  95. if i == 0:
  96. title = 'MEETING PROBABILITIES'
  97. i = i + 1
  98. continue
  99. if i == 1:
  100. i = i + 1
  101. table_th_list = tr.find_elements(By.TAG_NAME, "th")
  102. row_list = []
  103. for th in table_th_list:
  104. row_list.append(th.text)
  105. if len(row_list) == 0:
  106. continue
  107. row_tuple = tuple(row_list)
  108. table_list.append(row_list)
  109. else:
  110. i = i + 1
  111. table_td_list = tr.find_elements(By.TAG_NAME, "td")
  112. row_list = []
  113. for td in table_td_list: # 遍历每一个td
  114. row_list.append(td.text) # 取出表格的数据,并放入行列表里
  115. if len(row_list) == 0:
  116. continue
  117. row_tuple = tuple(row_list)
  118. table_list.append(row_list)
  119. driver.quit()
  120. # list_text = content.strip().split('\n')
  121. # print(list_text)
  122. # ls = list()
  123. # title = ""
  124. # length = len(list_text)
  125. # for i in range(length):
  126. # line = list_text[i]
  127. # if i == 0:
  128. # title = line
  129. # continue
  130. # if i == 1:
  131. # line = line.replace('MEETING DATE', 'MEETING_DATE')
  132. # dataList = line.split(' ')
  133. # dataList[0] = 'MEETING DATE'
  134. # my_tuple = tuple(dataList)
  135. # ls.append(my_tuple) # 以元组的形式追加进空列表
  136. # continue
  137. # dataList = line.split(' ')
  138. # my_tuple = tuple(dataList)
  139. # ls.append(my_tuple) # 以元组的形式追加进空列表
  140. # 获取当前时间,并将其格式化为指定的形式
  141. current_time = datetime.datetime.now().strftime("%Y-%m-%d")
  142. # 构建新的文件路径
  143. book_name_xlsx = f'/home/code/python/meeting_probabilities/file/{current_time}.xlsx'
  144. # book_name_xlsx = f'/Users/xi/Desktop/{current_time}.xlsx'
  145. write_excel_xlsx(book_name_xlsx, title, table_list)
  146. url = 'http://47.102.213.75:8809/v1/test/resource/upload'
  147. file_path = book_name_xlsx # 替换为本地文件路径
  148. # file_path = '/Users/xi/Desktop/2023-10-15.xlsx' # 替换为本地文件路径
  149. print(file_path)
  150. print(datetime.datetime.now())
  151. response = send_file(url, file_path)
  152. print(response)