.NET Web 浏览器控件捕获下载
.NET Webbrowser Control Capture Downloads
如何抓取Webbrowser自动下载的下载文件(如.html、png等)
例如,如果网站通过 javascript 每 30 秒下载一个 html 文件,我如何使用网络浏览器控件捕获此 html?
我的方法是
第1步抓取脚本元素的内容(具有html文件路径的变量)
//using HtmlAgilityPack
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Considering first script tag (you need to check your decentands)
var script = doc.DocumentNode.Descendants()
.Where(n => n.Name == "script")
.First().InnerText;
// Return the data of the spect and stringify it into a proper JSON object
var engine = new Jurassic.ScriptEngine();
var result = engine.Evaluate("(function() { " + script + " return spects; })()");
var json = JSONObject.Stringify(engine, result);
Console.WriteLine(json);
Console.ReadKey();
第 2 步:在另一个 Web 浏览器控件或同一浏览器控件中打开 html 页面
WebBrowser wb2 = new WebBrowser();
wb2.AllowNavigation = true;
wb2.Navigate(jsVariableAsString);
第三步:保存浏览器页面
var html = wb2.DocumentText.ToString();
或wb2.ShowSaveAsDialog();
如果有效请告诉我。
如何抓取Webbrowser自动下载的下载文件(如.html、png等)
例如,如果网站通过 javascript 每 30 秒下载一个 html 文件,我如何使用网络浏览器控件捕获此 html?
我的方法是
第1步抓取脚本元素的内容(具有html文件路径的变量)
//using HtmlAgilityPack
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Considering first script tag (you need to check your decentands)
var script = doc.DocumentNode.Descendants()
.Where(n => n.Name == "script")
.First().InnerText;
// Return the data of the spect and stringify it into a proper JSON object
var engine = new Jurassic.ScriptEngine();
var result = engine.Evaluate("(function() { " + script + " return spects; })()");
var json = JSONObject.Stringify(engine, result);
Console.WriteLine(json);
Console.ReadKey();
第 2 步:在另一个 Web 浏览器控件或同一浏览器控件中打开 html 页面
WebBrowser wb2 = new WebBrowser();
wb2.AllowNavigation = true;
wb2.Navigate(jsVariableAsString);
第三步:保存浏览器页面
var html = wb2.DocumentText.ToString();
或wb2.ShowSaveAsDialog();
如果有效请告诉我。