metting.py 6.3 KB

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