【三】C# 基于WPF 使用CefSharp来做万能爬虫

目录

    【一】C# 基于WPF 使用CefSharp来做万能爬虫

    【二】C# 基于WPF 使用CefSharp来做万能爬虫

    【三】C# 基于WPF 使用CefSharp来做万能爬虫

    【四】C# 基于WPF 使用CefSharp来做万能爬虫---扩展 一个httpClient访问辅助类可设置代理

 

分享一个对CefSharpWebrowser的操作类

using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChromeControler
{
    /// 
    /// 对CefSharpWebBrowser的操作类
    /// 
    public static class CefWebBrowserControl
    {

        /// 
        /// 获取html内容
        /// 
        /// CefSharp的webbrowser
        /// 
        public static async Task GetHtmlSource(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser)
        {
            string htmlStr = string.Empty;
            await chromiumWebBrowser.GetSourceAsync().ContinueWith(taskHtml =>
            {
                htmlStr = taskHtml.Result;
            });
            return htmlStr;
        }

        /// 
        /// 给input赋值
        /// 
        /// CefSharp的webbrowser
        /// 根据id
        public static string SetValueById(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser, string id,string value)
        {
            if (id.Length > 0)
            {
                string js = @"(
                            function()
                            {
                                var ele= document.getElementById('" + id + @"');
                                if(ele!=null)
                                {
                                    ele.value='"+value+@"';
                                }
                            }
                           )()";
                ExecuteJs(chromiumWebBrowser, js);
                return "赋值成功";
            }
            else
                return "id不能为空";
        }

        /// 
        /// 给Input赋值
        /// 
        /// CefSharp的webbrowser
        /// 根据控件的名称
        /// 内部html包含***
        /// 外部html包含***
        public static string SetValueByName(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser, string name,string value, SearchType searchType = SearchType.ControlName, string innerHtml = "", string outHtml = "")
        {
            string js = string.Empty;
            string forStr = string.Empty;
            if (name.Length > 0)
            {
                if (innerHtml.Length > 0)
                {
                    forStr = "if(eleAug[i].innerText.indexOf('" + innerHtml + "') != -1) ";

                }
                if (outHtml.Length > 0)
                {
                    forStr = "if(eleAug[i].outerText.indexOf('" + outHtml + "') != -1) ";

                }
                string searchjs = string.Empty;
                switch (searchType)
                {
                    case SearchType.ControlName: { searchjs = " var eleAug= document.getElementsByName('" + name + @"');"; } break;
                    case SearchType.ClassName: { searchjs = " var eleAug= document.getElementsByClassName('" + name + @"');"; } break;
                    case SearchType.TagName: { searchjs = " var eleAug= document.getElementsByTagName('" + name + @"');"; } break;
                }
                js = @"(
                            function()
                            {
                               " + searchjs + @"
                                if(eleAug!=null)
                                {
                                    for(var i=0;i< eleAug.length;i++)
                                    {                                    
                                        " + forStr + @"
                                            { ele.value='" + value + @"';}
                                    }
                                }
                            }
                           )()";
            }
            else
            {
                return "名称不能为空";
            }
            ExecuteJs(chromiumWebBrowser, js);
            return "点击成功";
        }


        /// 
        /// 点击按钮
        /// 
        /// CefSharp的webbrowser
        /// 根据id
        public static string ClickButtonById(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser, string id)
        {
            if (id.Length > 0)
            {
                string js = @"(
                            function()
                            {
                                var ele= document.getElementById('" + id + @"');
                                if(ele!=null)
                                {
                                    ele.click();
                                }
                            }
                           )()";
                ExecuteJs(chromiumWebBrowser, js);
                return "点击成功";
            }
            else
                return "id不能为空";
        }

        /// 
        /// 点击按钮
        /// 
        /// CefSharp的webbrowser
        /// 根据控件的名称
        /// 内部html包含***
        /// 外部html包含***
        public static string ClickButtonByName(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser,  string name, SearchType searchType = SearchType.ControlName, string innerHtml = "", string outHtml = "")
        {
            string js = string.Empty;
            string forStr = string.Empty;
            if (name.Length > 0)
            {
                if (innerHtml.Length > 0)
                {
                    forStr = "if(eleAug[i].innerText.indexOf('" + innerHtml + "') != -1) ";

                }
                if (outHtml.Length > 0)
                {
                    forStr = "if(eleAug[i].outerText.indexOf('" + outHtml + "') != -1) ";

                }
                string searchjs = string.Empty;
                switch (searchType)
                {
                    case SearchType.ControlName: { searchjs = " var eleAug= document.getElementsByName('" + name + @"');"; } break;
                    case SearchType.ClassName: { searchjs = " var eleAug= document.getElementsByClassName('" + name + @"');"; } break;
                    case SearchType.TagName: { searchjs = " var eleAug= document.getElementsByTagName('" + name + @"');"; } break;
                }
                js = @"(
                            function()
                            {
                               " + searchjs + @"
                                if(eleAug!=null)
                                {
                                    for(var i=0;i< eleAug.length;i++)
                                    {                                    
                                        " + forStr + @"
                                            { eleAug[i].click();}
                                    }
                                }
                            }
                           )()";
            }
            else
            {
                return "名称不能为空";
            }
            ExecuteJs(chromiumWebBrowser, js);
            return "点击成功";
        }

        /// 
        /// 执行JS
        /// 
        /// wpf版本的ChromiumWebBrowser
        /// 
        public static void ExecuteJs(CefSharp.Wpf.ChromiumWebBrowser chromiumWebBrowser, string js)
        {
            var frame = chromiumWebBrowser.WebBrowser.GetMainFrame();
            frame.EvaluateScriptAsync(js);
        }
    }
    public enum SearchType
    {
        ControlName,
        ClassName,
        TagName

    }
}

使用示例:

  CefWebBrowserControl.SetValueById(chromiumWebBrowser, "kw", "万能爬虫");
  CefWebBrowserControl.ClickButtonById(chromiumWebBrowser, "su");
  CefWebBrowserControl.ClickButtonByName(chromiumWebBrowser, "n", SearchType.ClassName, "下一页>");

你可能感兴趣的:(WPF,CefSharp)