我正在嘗試使用 selenium & python從NSE-India網站下載每日報告。
下載每日報告的方法
- 沒有資料的網站加載
- X 時間后,頁面加載報告資訊
- 一旦頁面加載了報表資料,就會出現“table[@id='etfTable']”
- 代碼中添加顯式等待,等待“table[@id='etfTable']”加載
顯式等待代碼
element=WebDriverWait(driver,50).until(EC.visibility_of_element_located(By.xpath,"//table[@id='etfTable']"))
使用 xpath 提取 onclick 事件
downloadcsv= driver.find_element_by_xpath("//div[@id='esw-etf']/div[2]/div/div[3]/div/ul/li/a")觸發點擊下載檔案
完整代碼
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options =webdriver.ChromeOptions(); prefs={"download.default_directory":"/Volumes/Project/WebScrapper/downloadData"}; options.binary_location=r'/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome' chrome_driver_binary =r'/usr/local/Caskroom/chromedriver/94.0.4606.61/chromedriver' options.add_experimental_option("prefs",prefs) driver =webdriver.Chrome(chrome_driver_binary,options=options) try: #driver.implicity_wait(10) driver.get('https://www.nseindia.com/market-data/exchange-traded-funds-etf') element =WebDriverWait(driver,50).until(EC.visibility_of_element_located(By.xpath,"//table[@id='etfTable']")) downloadcsv= driver.find_element_by_xpath("//div[@id='esw-etf']/div[2]/div/div[3]/div/ul/li/a") print(downloadcsv) downloadcsv.click() time.sleep(5) driver.close() except: print("Invalid URL")
我面臨的問題。
- 該頁面不斷加載,但在沒有硒的情況下啟動時,每日報告正在加載
通過硒正常加載
- 無法下載每日報告
uj5u.com熱心網友回復:
程式中有一些語法錯誤。就像幾行中的分號一樣,在查找elementusing 時WebDriverWait,缺少括號。
嘗試如下并確認。
可以使用 Javascript 單擊該元素。
driver.get("https://www.nseindia.com/market-data/exchange-traded-funds-etf")
element =WebDriverWait(driver,50).until(EC.visibility_of_element_located((By.XPATH,"//table[@id='etfTable']/tbody/tr[2]")))
downloadcsv= driver.find_element_by_xpath("//img[@title='csv']/parent::a")
print(downloadcsv)
driver.execute_script("arguments[0].click();",downloadcsv)
uj5u.com熱心網友回復:
這不是您的代碼的問題,而是網站的問題。我大部分時間都檢查過它不允許我點擊 CSV 檔案。您可以抓取表格,而不是下載 CSV 檔案。
# for direct to the page delete cookies is very important otherwise it will deny the access
browser.delete_all_cookies()
browser.get('https://www.nseindia.com/market-data/exchange-traded-funds-etf')
sleep(5)
soup = BeautifulSoup(browser.page_source, 'html.parser')
# scrape the table from the soup
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315540.html
上一篇:NUnitFrameworkAssertionException沒有明確的原因
下一篇:按鈕未點擊selinium
