在爬虫的时候会遇到下载文件的情况,这时候如果用Chrome浏览器点击下载,文件会自动存放到默认文件夹,一般是 我的电脑>下载 这个路径,如果我们想下载到指定文件夹,有没有办法呢?,可以试试下面的方法,在启动driver的时候就指定一个默认下载路径

from selenium import webdriver
options = webdriver.ChromeOptions()
out_path = r'D:\Projects\Spiders'  # 是你想指定的路径
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': out_path}
options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(executable_path=r'D:\Repo 3\chromedriver.exe', chrome_options=options)

更多推荐

Python selenium调用Chrome driver更改文件默认下载位置