PhantomJs自動化請求頁面,加入了重啟機制避免記憶體耗盡
阿新 • • 發佈:2022-04-08
private static PhantomJSDriverService GetPhantomJSDriverService() { PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService("D:/");//Phantomjs.exe 需要放到指定的路徑下面 pds.LoadImages = false; pds.HideCommandPromptWindow = true; pds.IgnoreSslErrors= true; //設定代理伺服器地址 //pds.Proxy = $"{ip}:{port}"; //設定代理伺服器認證資訊 //pds.ProxyAuthentication = GetProxyAuthorization(); return pds; } private static void RunTask(Dictionary<string, DataTable> dic) { IWebDriver driver= new PhantomJSDriver(GetPhantomJSDriverService()); foreach (var urlTemplate in dic.Keys) { DataTable dt = dic[urlTemplate]; int times = 0; foreach (DataRow dr in dt.Rows) { times++;if (times % 50 == 0) { driver.Quit(); driver = new PhantomJSDriver(GetPhantomJSDriverService()); Console.WriteLine("重啟瀏覽器"); } var url = string.Format(urlTemplate, Dc.ToInt(dr["ID"])); driver.Navigate().GoToUrl(url); try { Console.WriteLine(driver.Title); } catch (Exception ex) { Console.WriteLine("遇到異常:" + ex.Message); driver.Quit(); driver.Dispose(); driver = new PhantomJSDriver(GetPhantomJSDriverService()); } } } if (driver != null) { driver.Quit(); } } static ConsoleEventDelegate handler; // Keeps it from getting garbage collected static bool ConsoleEventCallback(int eventType) { if (eventType == 2) { List<Process> pList = Process.GetProcessesByName("phantomjs").ToList(); pList.ForEach(i => i.Kill()); } return false; } private delegate bool ConsoleEventDelegate(int eventType); [DllImport("kernel32.dll", SetLastError = true)] private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);