簡體   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