在python中的网站抓取网站

时间:2017-08-02 20:33:21

标签: python selenium parsing

在下面提到的网站上,当我选择日期为2017年6月27日,系列/运行费率为“USD RATES 1100”。提交后,费率将在该页面下方打开。直到这一点,我能够以程序方式做到这一点。但我需要上述日期和费率组合的10年率(答案是2.17)。有人可以告诉我我在代码的最后一行发生了什么错误。

https://www.theice.com/marketdata/reports/180

from selenium import webdriver
chrome_path = r"C:\Users\vick\Desktop\python_1\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.theice.com/marketdata/reports/180")
try: 
   driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/
   div[2]/button').click()
except:
      pass

driver.find_element_by_xpath('//*
[@id="seriesNameAndRunCode_chosen"]/a/span').click()
driver.find_element_by_xpath('//*
[@id="seriesNameAndRunCode_chosen"]/div/ul/li[5]').click()
driver.find_element_by_xpath('//*[@id="reportDate"]').clear()
driver.find_element_by_xpath('//*[@id="reportDate"]').send_keys("27-Jul-
2017") 
driver.find_element_by_xpath('//*[@id="selectForm"]/input').click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)/2;")
print(driver.find_element_by_xpath('//*[@id="report-
content"]/div/div/table/tbody/tr[10]/td[2]').get_attribute('innerHTML'))

我在最后一行收到错误: NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// * [@ id =”report-content“] / div / div / table / tbody / tr [10 ] / TD [2]“}

谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

单击输入字段时,您需要等待一两秒钟。像:

from selenium import webdriver
chrome_path = r"C:\Users\vick\Desktop\python_1\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.theice.com/marketdata/reports/180")
try: 
   driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[2]/button').click()
except:
      pass

driver.find_element_by_xpath('//*[@id="seriesNameAndRunCode_chosen"]/a/span').click()
driver.find_element_by_xpath('//*[@id="seriesNameAndRunCode_chosen"]/div/ul/li[5]').click()
driver.find_element_by_xpath('//*[@id="reportDate"]').clear()
driver.find_element_by_xpath('//*[@id="reportDate"]').send_keys("27-Jul-2017") 
driver.find_element_by_xpath('//*[@id="selectForm"]/input').click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)/2;")
time.sleep(2) #here is the part where you should wait. 
print(driver.find_element_by_xpath('//*[@id="report-content"]/div/div/table/tbody/tr[10]/td[2]').get_attribute('innerHTML'))

选项B要等到元素加载完毕:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException

....
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)/2;")
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'report-content'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
......
print(driver.find_element_by_xpath('//*[@id="report-content"]/div/div/table/tbody/tr[10]/td[2]').get_attribute('innerHTML'))

在第一种情况下,Python等待2秒而不是继续。 在第二种情况下,Webdriver等待元素加载(最多5秒)

尝试了代码并且它有效。希望有所帮助。