繁体   English   中英

从 CefSharp Web 浏览器获取 HTML 源代码

[英]Get HTML source code from CefSharp web browser

我正在使用 aCefSharp.Wpf.ChromiumWebBrowser(版本 47.0.3.0)加载网页。 页面加载后的某个时间点我想获取源代码。

我打电话给:

wb.GetBrowser().MainFrame.GetSourceAsync()

但是它似乎没有返回所有源代码(我相信这是因为有子框架)。

如果我打电话:

wb.GetBrowser().MainFrame.ViewSource() 

我可以看到它列出了所有源代码(包括内部框架)。

我想得到与 ViewSource() 相同的结果。 请有人指出我正确的方向吗?

更新 - 添加了代码示例

注意:Web 浏览器指向的地址也仅适用于 2016 年 3 月 10 日(含)。 之后它可能会显示不同的数据,这不是我要查看的。

在 frmSelection.xaml 文件中

<cefSharp:ChromiumWebBrowser Name="wb" Grid.Column="1" Grid.Row="0" />

在 frmSelection.xaml.cs 文件中

public partial class frmSelection : UserControl
{
    private System.Windows.Threading.DispatcherTimer wbTimer = new System.Windows.Threading.DispatcherTimer();

    public frmSelection()
    {

         InitializeComponent();

         // This timer will start when a web page has been loaded.
         // It will wait 4 seconds and then call wbTimer_Tick which 
         // will then see if data can be extracted from the web page.
         wbTimer.Interval = new TimeSpan(0, 0, 4);
         wbTimer.Tick += new EventHandler(wbTimer_Tick);

         wb.Address = "http://www.racingpost.com/horses2/cards/card.sd?race_id=644222&r_date=2016-03-10#raceTabs=sc_";

         wb.FrameLoadEnd += new EventHandler<CefSharp.FrameLoadEndEventArgs>(wb_FrameLoadEnd);

    }

        void wb_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
        {
            if (wbTimer.IsEnabled)
                wbTimer.Stop();

            wbTimer.Start();
        }

    void wbTimer_Tick(object sender, EventArgs e)
    {
        wbTimer.Stop();
        string html = GetHTMLFromWebBrowser();
    }

    private string GetHTMLFromWebBrowser()
    {
         // call the ViewSource method which will open up notepad and display the html.
         // this is just so I can compare it to the html returned in GetSourceAsync()
         // This is displaying all the html code (including child frames)
            wb.GetBrowser().MainFrame.ViewSource();

         // Get the html source code from the main Frame.
            // This is displaying only code in the main frame and not any child frames of it.
            Task<String> taskHtml = wb.GetBrowser().MainFrame.GetSourceAsync();

            string response = taskHtml.Result;
     return response;
  }

}

我认为我不太了解这个DispatcherTimer解决方案。 我会这样做:

public frmSelection()
{
    InitializeComponent();

    wb.FrameLoadEnd += WebBrowserFrameLoadEnded;
    wb.Address = "http://www.racingpost.com/horses2/cards/card.sd?race_id=644222&r_date=2016-03-10#raceTabs=sc_";
}

private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    if (e.Frame.IsMain)
    {
        wb.ViewSource();
        wb.GetSourceAsync().ContinueWith(taskHtml =>
        {
            var html = taskHtml.Result;
        });
    }
}

我对ViewSource的输出和html变量中的文本做了一个差异,它们是相同的,所以我无法在这里重现您的问题。

这就是说,我注意到主框架加载很晚,所以你必须等待一段时间,直到记事本弹出源。

我在尝试点击位于框架中而不是主框架中的项目时遇到了同样的问题。 使用您的答案中的示例,我编写了以下扩展方法:

        public static IFrame GetFrame(this ChromiumWebBrowser browser, string FrameName)
    {
        IFrame frame = null;

        var identifiers = browser.GetBrowser().GetFrameIdentifiers();

        foreach (var i in identifiers)
        {
            frame = browser.GetBrowser().GetFrame(i);
            if (frame.Name == FrameName)
                return frame;
        }

        return null;
    }

如果您的表单上有包含此方法的模块的“使用”,您可以执行以下操作:

var frame = browser.GetFrame("nameofframe");
        if (frame != null)
        {
            string HTML = await frame.GetSourceAsync();
        }

当然在使用这个之前你需要确保页面加载完成,但我打算大量使用它。 希望能帮助到你!

吉姆

CefSharp with this Code with source code 你可以拿

    public ChromiumWebBrowser syhmhfzdrv;
    async Task<string>  Bekraanlizying()
    {
        string syhmhfzhtml = await syhmhfzdrv.GetSourceAsync();
        return syhmhfzhtml;
    }

来自 iframe 的源代码以获取此代码

    async Task<string> syhmhfziframesourcecode()
    {
        string syhmhfzHtml = "";
        var identifiers = drv.GetBrowser().GetFrameIdentifiers();

        foreach (var i in identifiers)
        {
            IFrame frame = drv.GetBrowser().GetFrame(i);
            if (frame.Name == "frmMain")//Write the name iframe..
            {
                syhmhfzHtml = await frame.GetSourceAsync();
            }
        }
        return syhmhfzHtml;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM