guangzhou_qihuo.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import base64
  2. import os
  3. import time
  4. from time import sleep
  5. import numpy as np
  6. from PIL import Image
  7. from selenium import webdriver
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from selenium.webdriver import ActionChains
  10. from selenium.webdriver.chrome.options import Options
  11. from selenium.common.exceptions import WebDriverException
  12. # 设置Chrome浏览器选项
  13. from selenium.webdriver.chrome.service import Service
  14. # 调用截图函数
  15. from selenium.webdriver.common.by import By
  16. from selenium.webdriver.support.wait import WebDriverWait
  17. if __name__ == "__main__":
  18. # 创建一个 Chrome WebDriver 实例
  19. options = webdriver.ChromeOptions()
  20. # options.add_argument("headless")
  21. # options.add_argument(" window-size=1920,1080")
  22. prefs = {'download.prompt_for_download': False, 'download.default_directory': '/Users/xiexiaoyuan/Downloads/'}
  23. options.add_experimental_option('prefs', prefs)
  24. s = Service(executable_path='/Users/xiexiaoyuan/chromedriver_mac64_111/chromedriver')
  25. driver = webdriver.Chrome(service=s, options=options)
  26. # driver.maximize_window()
  27. driver.get('http://www.gfex.com.cn/gfex/rihq/hqsj_tjsj.shtml')
  28. driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  29. "source": """
  30. Object.defineProperty(navigator, 'webdriver', {
  31. get: () => undefined
  32. })
  33. """
  34. })
  35. # sleep(2)
  36. # WebDriverWait(driver, 10).until(
  37. # EC.element_to_be_clickable((By.XPATH, '//*[@lay-ymd="2023-10-25"]'))).click()
  38. sleep(2)
  39. WebDriverWait(driver, 10).until(
  40. EC.element_to_be_clickable((By.XPATH, '//div[@id="tradeType"]/ul/li[1]/a'))).click()
  41. sleep(5)
  42. WebDriverWait(driver, 10).until(
  43. EC.element_to_be_clickable((By.XPATH, '//button[@id="export_excel"]'))).click() # 点击导出excel
  44. # b = driver.find_element(By.XPATH, '//button[@id="export_excel"]')
  45. # print(b.text)
  46. # b.click()
  47. sleep(10)
  48. # 等待文件下载完成
  49. driver.implicitly_wait(10) # 等待10秒
  50. driver.quit()