简体   繁体   English

用python网站抓取网页

[英]web scraping of website in python

On the below mentioned website, When I select date as 27 jun-2017 and Series/Run rates as "USD RATES 1100". 在下面提到的网站上,当我选择“日期为2017年6月27日”和“系列/运行率”为“美元汇率1100”时。 After submitting it, rates opens below on that page. 提交后,费率将在该页面下方打开。 Till this point I am able to do it programitically. 到此为止,我可以通过编程方式进行操作。 But I need 10 year rate(answer is 2.17) of above mentioned date and rate combination. 但是我需要上述日期和费率组合的10年费率(答案为2.17)。 Can some one please tell me what error I am making in the last line of the code. 有人可以告诉我我在代码的最后一行中犯了什么错误。

https://www.theice.com/marketdata/reports/180 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'))

Error I am getting in last line: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="report-content"]/div/div/table/tbody/tr[10]/td[2]"} 错误,我进入最后一行:NoSuchElementException:没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // * [@ id =” report-content“] / div / div /表/ tbody的/ TR [10] / TD [2]“}

Thankyou for the help 感谢您的帮助

You have to wait a second or two when you click the input field. 单击输入字段时,您必须等待一两秒钟。 Like: 喜欢:

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'))

Option B is to wait until the element has been loaded: 选项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'))

In the first case Python waits 2 seconds and than continues. 在第一种情况下,Python等待2秒,然后继续。 In the second case the Webdriver waits until the element is loaded (for maximal 5 seconds) 在第二种情况下,Webdriver等待直到元素被加载(最多5秒钟)

Tried the code and it works. 尝试了代码,它可以工作。 Hope that helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM