zoukankan      html  css  js  c++  java
  • CefSharp获取页面Html代码的两种方式

    CefSharp在NuGet的简介是“The CefSharp Chromium-based browser component”,机翻的意思就是“基于Cefsharp Chromium的浏览器组件”

    第一种方法 就是执行JavaScript代码获取当前html代码

    复制代码
    StringBuilder sb = new StringBuilder();
                        sb.AppendLine("function tempFunction() {");
                        //sb.AppendLine(" return document.body.innerHTML; "); 
                        sb.AppendLine(" return document.getElementsByTagName('html')[0].innerHTML; ");
                        sb.AppendLine("}");
                        sb.AppendLine("tempFunction();");
                        var task01 = browser.GetBrowser().GetFrame(browser.GetBrowser().GetFrameNames()[0]).EvaluateScriptAsync(sb.ToString());
                        task01.ContinueWith(t =>
                        {
                            if (!t.IsFaulted)
                            {
                                var response = t.Result;
                                if (response.Success == true)
                                {
                                    if (response.Result != null)
                                    {
                                        string resultStr = response.Result.ToString();
                                    }
                                }
                            }
                        });
    复制代码

    第二种方法 是利用CefSharp.IFrame.GetSourceAsync()方法

    复制代码
    /// <summary>
            /// 页面加载结束
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
            {
                var task02 = e.Frame.GetSourceAsync();
                task02.ContinueWith(t =>
                {
                    if (!t.IsFaulted)
                    {
                        string resultStr = t.Result;
                    }
                });
            }
    复制代码

    我这里是在Browser_FrameLoadEnd事件中直接获取的IFrame,如下:

        ChromWebBrowser = new CefSharp.WinForms.ChromiumWebBrowser(url);
        ChromWebBrowser.FrameLoadEnd += ChromWebBrowser_FrameLoadEnd;

    GetSourceAsync()方法我简单翻译了一下

    复制代码
            //
            // 摘要:
            //     Retrieve this frame's HTML source as a string sent to the specified visitor.
            //     检索此框架的HTML源代码以字符串形式发送给指定访问者。
            //
            // 返回结果:
            //     a System.Threading.Tasks.Task`1 that when executed returns this frame's HTML
            //     source as a string.
            //     一个线程任务,执行时将此框架的HTML源文件作为字符串返回。
            Task<string> GetSourceAsync();
    复制代码

    出处:https://www.cnblogs.com/leiyongbo/p/10484791.html

    您的资助是我最大的动力!
    金额随意,欢迎来赏!
    款后有任何问题请给我留言。

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我。(●'◡'●)

    如果你觉得本篇文章对你有所帮助,请给予我更多的鼓励,求打             付款后有任何问题请给我留言!!!

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【Jack_孟】!

  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/mq0036/p/15439202.html
Copyright © 2011-2022 走看看