【问题标题】:Not able to download the file from websiteusing selenium python无法使用 selenium python 从网站下载文件
【发布时间】:2021-12-01 13:57:57
【问题描述】:

我正在尝试使用 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/WebScraper/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")

我面临的问题。

  • 页面一直在加载,但在不使用 selenium 的情况下启动时,会加载每日报告

Normal Loading via Selenium

  • 无法下载每日报告

【问题讨论】:

  • 正如@Darkknight/@pmadhu 所提到的,网站有一些机器人检测,导致“403”响应。能够在 undetected_chromedriver的帮助下绕过机器人检测>欲了解更多信息[stackoverflow.com/questions/65529808/…

标签: python selenium selenium-webdriver download


【解决方案1】:

程序中有一些语法错误。就像几行中的分号,在使用WebDriverWait 查找element 时,缺少括号

尝试如下并确认。

可以使用 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)

【讨论】:

  • 我更新了代码(删除了几行中的分号),但页面没有加载数据。由于我无法在评论中附加屏幕截图,我正在更新问题带有屏幕截图信息
  • @Rovesprite - 已更新代码。应用等待,以便表中存在一些行。并确保您有良好的互联网速度。我能够看到加载了数据的表格。
  • 等待表中的行而不是 id 是一个很好的解决方案,但我仍然无法下载文件。问题是它有时是中间数据加载但大部分时间它是页面已加载。执行代码时使用无头浏览器选项。 ?
  • @Rovesprite - 我没有应用任何选项。几个小时前能够点击元素。但现在我也无法点击该元素。刷新时获取拒绝访问消息。可能是网站不允许自动化。
【解决方案2】:

这不是您的代码的问题,而是网站的问题。我检查了它大部分时间它不允许我点击 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

【讨论】:

  • 我厌倦了您建议的选项建议,但未加载表格信息。我检查了网络选项卡,加载数据的 GET 方法返回“403”。需要弄清楚它为什么抛出“403”
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2016-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2019-05-09
  • 1970-01-01
相关资源
最近更新 更多