[英]mshtml.HTMLDocument how to hide a dynamically created div with class attribute
[英]how to dynamically generate HTML code using .NET's WebBrowser or mshtml.HTMLDocument?
我已阅读的有关该主题的大多数答案都指向System.Windows.Forms.WebBrowser类或Microsoft HTML对象库程序集的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);
}
以上仅是示例。 我不是真的想找到一种解决方法来弄清楚我所在的城镇的名称。 我只需要了解如何以编程方式检索这种动态生成的数据即可。
(调用新的System.Net.WebClient.DownloadString(“ https://www.google.com/#q=where+am+i ”),将生成的文本保存在某处,搜索您当前所在城镇的名称找到,让我知道您是否能够找到它。)
但是,当我从网络浏览器(即firefox)访问“ https://www.google.com/#q=where+am+i ”时,我看到的城镇名称写在网页上。 在Firefox中,如果我右键单击城镇名称并选择“检查元素(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);
}
}
我认为必须有一种优雅的方式来获取此类信息。 现在,我所能想到的就是将WebBrowser控件添加到表单中,让它导航到相关URL,发送键“ CLRL,A”,然后将页面上显示的所有内容复制到剪贴板,然后尝试解析它。 但是,这是一个可怕的解决方案。
我想为Alexei的答案贡献一些代码。 几点:
严格来说,并非总是可以确定页面何时以100%的概率完成渲染。 一些页面非常复杂,并使用连续的AJAX更新。 但是,通过轮询页面的当前HTML快照进行更改并检查WebBrowser.IsBusy
属性,我们可以非常接近。 这就是LoadDynamicPage
在下面执行的操作。
在页面上方永无休止的情况下,必须在上面加上一些超时逻辑(请注意CancellationTokenSource
)。
Async/await
是对此进行编码的好工具,因为它为我们的异步轮询逻辑提供了线性代码流,从而大大简化了它。
使用浏览器功能控件启用HTML5渲染很重要,因为WebBrowser
默认在IE7仿真模式下运行。 这就是SetFeatureBrowserEmulation
在下面执行的操作。
这是一个WinForms应用程序,但是可以轻松地将该概念转换为控制台应用程序 。
此逻辑在您专门提到的URL上很好用: 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);
}
}
}
您的网络浏览器代码看起来很合理-等待获取当前内容的内容。 不幸的是,浏览器或JavaScript都没有正式的“我已经执行完JavaScript,可以随意窃取内容”的通知了。
某种活动等待(不是Sleep
而是Timer
)可能是必需的,并且是特定于页面的。 即使您使用无头浏览器(即PhantomJS),也会遇到相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.