如何使用.NET的WebBrowser或mshtml.HTMLDocument动态生成HTML代码?

12
大多数我读到的关于这个主题的答案指向System.Windows.Forms.WebBrowser类或Microsoft HTML Object Library程序集中的COM接口mshtml.HTMLDocument。
WebBrowser类并没有帮助我。以下代码无法检索到由我的网络浏览器呈现的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);
} 

上面只是一个例子。我真正感兴趣的不是找出我所在城镇的解决方法。我只需要了解如何以编程方式检索那种动态生成的数据。
(调用 new System.Net.WebClient.DownloadString("https://www.google.com/#q=where+am+i"),将结果保存在某个地方,查找您当前所在位置的城镇名称,然后告诉我您是否能找到它。)
但是当我从我的Web浏览器(IE或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"键,将页面上显示的任何内容复制到剪贴板并尝试解析。然而,这是一个可怕的解决方案。

2个回答

19

我想为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://dev59.com/VmMl5IYBdhLWcg3wgnWl#18333982
        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);
        }
    }
}

1
我不得不添加到CancellationTokenSource中使用的时间间隔以适应我的目的,但除此之外,这个解决方案非常好!我尝试了很多其他的“解决方案”,但这是唯一一个真正解决了我的问题。 - majestzim

5

你的网页浏览器代码看起来很合理 - 等待某些内容,然后抓取当前内容。不幸的是,浏览器和JavaScript都没有官方的“我执行完JavaScript了,随便偷内容”的通知。

可能需要某种主动等待(不是Sleep而是Timer),并且需要根据页面具体情况进行设置。即使使用无头浏览器(如PhantomJS),您也将面临同样的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接