需要安装好Python环境,以下基于Python3实现,Windows10环境: 所需Python的拓展库,需要用到selenium库对网页元素进行操作;安装chrome浏览器,下载并解压相对应版本的谷歌驱动:

  from selenium import webdriver
  import time
  from selenium.webdriver.common.by import By
  import os
  from selenium.webdriver.chrome.options import Options #安装chrome,下载解压Chromediver

以下是主体部分:

  env_dist = os.environ # environ是在os.py中定义的一个dict environ = {}
  usdtxt=env_dist.get('usd_hlfile_path')#环境变量usd_hlfile_path,存放usd汇率的路径下txt文本
  print (usdtxt)
  rmbtxt=env_dist.get('rmb_hlfile_path')#环境变量rmb_hlfile_path,存放rmb汇率的路径下txt文本
  print (rmbtxt)
  chromedriver =env_dist.get('chromedriver_path')#环境变量chromedriver_path,存放谷歌驱动的路径
  print(chromedriver)

  chrome_options = Options()
  chrome_options.add_argument('--no-sandbox')
  chrome_options.add_argument('--disable-dev-shm-usage')
  chrome_options.add_argument('--headless')
  driver = webdriver.Chrome(options=chrome_options)

  #driver=webdriver.Chrome(executable_path=chromedriver)
  driver.maximize_window()
  driver.get("https://huobiduihuan.bmcx.com/CNY__huobiduihuan/")
  time.sleep(2)
  try:
      hl=driver.find_element(By.XPATH, '//*[@id="main_content"]/table/tbody/tr/td/table/tbody')
      print(hl.text)
      print('ok')
  except Exception as e:
      print('no')
  time.sleep(2)
  hlfile=open(rmbtxt,'w+',encoding='utf8')
  hlfile.write(hl.text)
  hlfile.close()
  #driver.close()
  #driver=webdriver.Chrome(executable_path=chromedriver)
  driver.maximize_window()
  driver.get("https://huobiduihuan.bmcx.com/USD__huobiduihuan/")
  time.sleep(2)
  try:
      hl=driver.find_element(By.XPATH, '//*[@id="main_content"]/table/tbody/tr/td/table/tbody')
      print(hl.text)
      print('ok')
  except Exception as e:
      print('no')
  time.sleep(2)
  hlfile=open(usdtxt,'w+',encoding='utf8')
  hlfile.write(hl.text)
  hlfile.close()
  driver.close()