繁体   English   中英

用python网站抓取网页

[英]web scraping of website in python

在下面提到的网站上,当我选择“日期为2017年6月27日”和“系列/运行率”为“美元汇率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 /表/ tbody的/ TR [10] / TD [2]“}

感谢您的帮助

单击输入字段时,您必须等待一两秒钟。 喜欢:

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秒钟)

尝试了代码,它可以工作。 希望能有所帮助。

暂无
暂无

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

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