J S*_*ith 12 html javascript c# webbrowser-control dynamic-html
我读过的关于这个主题的大多数答案都指向System.Windows.Forms.WebBrowser类或来自Microsoft HTML Object Library程序集的COM接口mshtml.HTMLDocument.
WebBrowser类没有引导我到任何地方.以下代码无法检索由我的Web浏览器呈现的HTML代码:
[STAThread]
public static void Main()
{
WebBrowser wb = new WebBrowser();
wb.Navigate("https://www.google.com/#q=where+am+i");
wb.DocumentCompleted += delegate(object sender, WebBrowserDocumentCompletedEventArgs e)
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document.DomDocument;
foreach (IHTMLElement element in doc.all)
{
System.Diagnostics.Debug.WriteLine(element.outerHTML);
}
};
Form f = new Form();
f.Controls.Add(wb);
Application.Run(f);
}
Run Code Online (Sandbox Code Playgroud)
以上只是一个例子.我真的不想找到一个解决方法来找出我所在城镇的名称.我只需要了解如何以编程方式检索那种动态生成的数据.
(调用新的System.Net.WebClient.DownloadString(" https://www.google.com/#q=where+am+i "),将生成的文本保存到某处,搜索您当前所在城镇的名称找到了,如果你能找到它,请告诉我.)
但是,当我从我的网络浏览器(即或Firefox)访问" https://www.google.com/#q=where+am+i "时,我会在网页上看到我的城镇名称.在Firefox中,如果我右键单击城镇名称并选择"Inspect Element(Q)",我会清楚地看到用HTML代码编写的城镇名称,这看起来与WebClient返回的原始HTML完全不同.
在我厌倦了玩System.Net.WebBrowser后,我决定给mshtml.HTMLDocument一个镜头,最后得到同样无用的原始HTML:
public static void Main()
{
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)new mshtml.HTMLDocument();
doc.write(new System.Net.WebClient().DownloadString("https://www.google.com/#q=where+am+i"));
foreach (IHTMLElement e in doc.all)
{
System.Diagnostics.Debug.WriteLine(e.outerHTML);
}
}
Run Code Online (Sandbox Code Playgroud)
我想必须有一种优雅的方式来获取这种信息.现在,我能想到的是将一个WebBrowser控件添加到表单中,让它导航到相关的URL,发送键"CLRL,A",并将页面上显示的任何内容复制到剪贴板并尝试解析它.不过,这是一个可怕的解决方案.
nos*_*tio 18
我想为阿列克谢的回答贡献一些代码.几点:
严格地说,可能无法始终确定页面何时以100%的概率完成渲染.有些页面非常复杂,并且使用连续的AJAX更新.但是我们可以通过轮询页面的当前HTML快照以获取更改并检查WebBrowser.IsBusy属性来完全接近.这就是
LoadDynamicPage下面的内容.
如果页面呈现永无止境(注意CancellationTokenSource),则必须在上面提供一些超时逻辑.
Async/await 是一个很好的编码工具,因为它为我们的异步轮询逻辑提供了线性代码流,大大简化了它.
使用浏览器功能控件启用HTML5渲染非常重要,因为WebBrowser默认情况下在IE7仿真模式下运行.这就是SetFeatureBrowserEmulation下面的内容.
这是一个WinForms应用程序,但该概念可以很容易地转换为控制台应用程序.
此逻辑适用于您特别提到的网址:https://www.google.com/#q=where+am+i.
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WbFetchPage
{
public partial class MainForm : Form
{
public MainForm()
{
SetFeatureBrowserEmulation();
InitializeComponent();
this.Load += MainForm_Load;
}
// start the task
async void MainForm_Load(object sender, EventArgs e)
{
try
{
var cts = new CancellationTokenSource(10000); // cancel in 10s
var html = await LoadDynamicPage("https://www.google.com/#q=where+am+i", cts.Token);
MessageBox.Show(html.Substring(0, 1024) + "..." ); // it's too long!
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// navigate and download
async Task<string> LoadDynamicPage(string url, CancellationToken token)
{
// navigate and await DocumentCompleted
var tcs = new TaskCompletionSource<bool>();
WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
tcs.TrySetResult(true);
using (token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: true))
{
this.webBrowser.DocumentCompleted += handler;
try
{
this.webBrowser.Navigate(url);
await tcs.Task; // wait for DocumentCompleted
}
finally
{
this.webBrowser.DocumentCompleted -= handler;
}
}
// get the root element
var documentElement = this.webBrowser.Document.GetElementsByTagName("html")[0];
// poll the current HTML for changes asynchronosly
var html = documentElement.OuterHtml;
while (true)
{
// wait asynchronously, this will throw if cancellation requested
await Task.Delay(500, token);
// continue polling if the WebBrowser is still busy
if (this.webBrowser.IsBusy)
continue;
var htmlNow = documentElement.OuterHtml;
if (html == htmlNow)
break; // no changes detected, end the poll loop
html = htmlNow;
}
// consider the page fully rendered
token.ThrowIfCancellationRequested();
return html;
}
// enable HTML5 (assuming we're running IE10+)
// more info: https://stackoverflow.com/a/18333982/1768303
static void SetFeatureBrowserEmulation()
{
if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
return;
var appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
appName, 10000, RegistryValueKind.DWord);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的网络浏览器代码看起来很合理 - 等待获取当前内容的内容.不幸的是,没有官方的"我已经完成了执行JavaScript,随意窃取内容"来自浏览器和JavaScript的通知.
某些活动等待(不是Sleep但Timer可能)是必要的并且是特定于页面的.即使您使用无头浏览器(即PhantomJS),您也会遇到同样的问题.