我并不是真正的 Python 用户,但我正在使用一些从网上获取的代码来下载文件。其中代码之一是:
urlpage = 'https://www150.statcan.gc.ca/n1/tbl/csv/' + '10100127' + '-eng.zip'
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'D:\downloads')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox()
driver.get(urlpage)
据我所知,应该只是将文件下载到我的 D: 驱动器的下载文件夹中,但是当我运行代码时,网页会打开,然后询问我是否要查看或下载该文件。代码有什么问题吗?还是我做错了什么?
不确定这是否是重要信息,但我正在使用 PyCharm 作为我的 IDE
最佳答案
这是您应该使用的脚本,这会将文件保存在系统默认下载文件夹中。
FF_options = webdriver.FirefoxProfile()
FF_options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/zip")
driver= webdriver.Firefox(firefox_profile=FF_options)
如果您想将下载的文件保存在特定位置,请添加以下首选项。
# change the path here, current line will save in the working directory meaning
# the location where your script is.
FF_options.set_preference("browser.download.dir", os.getcwd())
FF_options.set_preference("browser.download.folderList",2)
关于python - 当我已经在 Python 中设置 Saves 时,为什么 Selenium 仍然要求我配置 Saves?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56991061/