cefsharp 循环从多个页面提取数据
foreach (var url in urls) { var loaded = false; Action<object, FrameLoadEndEventArgs> frameLoadEndProc = (s, args) => { if (args.Frame.IsMain && args.Frame.Url == url.Value) { loaded = true; } }; var frameLoadEndEventHandler = new EventHandler<FrameLoadEndEventArgs>(frameLoadEndProc); // 订阅事件处理程序 browser.FrameLoadEnd += frameLoadEndEventHandler; //加载url并等待完成 browser.Load(url); var start = DateTime.Now; while (!loaded) { //最多加载10s if (DateTime.Now <= start + TimeSpan.FromSeconds(10)) { Application.DoEvents(); continue; } break; } //your code here // 取消订阅事件处理程序 browser.FrameLoadEnd -= frameLoadEndEventHandler; }
下面是一个真实完整示例:
var url = "https://url.com"; var firstLoad = true; while (true) { var loaded = false; Action<object, FrameLoadEndEventArgs> frameLoadEndProc = (s, args) => { if (args.Frame.IsMain && args.Frame.Url == url) { loaded = true; } }; var frameLoadEndEventHandler = new EventHandler<FrameLoadEndEventArgs>(frameLoadEndProc); // 订阅事件处理程序 browser.FrameLoadEnd += frameLoadEndEventHandler; if (firstLoad) { firstLoad = false; //加载url并等待完成 browser.Load(url); } else { var response = await browser.EvaluateScriptAsync("$('a:contains(\"下一页\")').size()"); if (response.Success && response.Result?.ToString().Length > 0 && Convert.ToInt32(response.Result?.ToString()) > 0) { response = await browser.EvaluateScriptAsync("$('a:contains(\"下一页\")')[0].href"); if (response.Result?.ToString().ToUpper() == browser.Address.ToUpper()) break; else { url = response.Result?.ToString(); browser.Load(url); } } else break; } var start = DateTime.Now; while (!loaded) { //最多加载10s if (DateTime.Now <= start + TimeSpan.FromSeconds(10)) { Application.DoEvents(); continue; } break; } //提取页面数据 // 取消订阅事件处理程序 browser.FrameLoadEnd -= frameLoadEndEventHandler; }
桂棹兮兰桨,击空明兮溯流光。