??使用selenium進(jìn)行爬蟲開發(fā)時(shí)郭怪,會(huì)遇到下載文件時(shí)彈窗顯示保存還是打開的問題,這時(shí)候可以通過代碼設(shè)置自動(dòng)下載爬凑。
設(shè)置文件自動(dòng)下載
通過網(wǎng)頁開發(fā)工具分析文件的http響應(yīng)的contentType頭類型如:
Content-Type: vnd.ms-excel
這時(shí)候在代碼中設(shè)置
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
設(shè)置文件下載路徑
profile.setPreference("browser.download.folderList", 2);
// 路徑分隔符g格式為雙斜杠\\,如E:\\sipder\\file
//所以需要做響應(yīng)的字符串處理
String downLoadPath = "E:\\sipder\\file"
profile.setPreference("browser.download.dir", downLoadPath );
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.download.manager.showWhenStarting", false);