• c# webview2获取网页HTML的绝招


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace vsphere
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                this.webView21.Source = new System.Uri(textBox1.Text, System.UriKind.Absolute);
            }
            private async void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
            {
     
                object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");//第一次获取没法获取后端的数据
                textBox2.Text = obj.ToString();
            }
     
            private async void button2_Click(object sender, EventArgs e)
            {
                object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");//等一会儿获取HTML就能看到后端数据了
                textBox2.Text = obj.ToString();
            }
     
     
     
            private async void button3_Click(object sender, EventArgs e)
            {
                object obj = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.innerText");//获取页面的文本
                textBox2.Text = obj.ToString();
            }
     
            private async void button4_Click(object sender, EventArgs e)
            {
                object obj = await webView21.CoreWebView2.ExecuteScriptAsync("$('.f3')[0].innerHTML");//jquery获取页面内容
                textBox2.Text = obj.ToString();
            }
        }
    }

    webView2 获取网页源代码

             private async void fManual_Load(object sender, EventArgs e)
            {
                if (webView21.CoreWebView2 == null)
                {
                   await webView21.EnsureCoreWebView2Async();
                    webView21.CoreWebView2.Settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56";
                }
                TextTool.Log(DateTime.Now + "  开始导航到" + url);
                webView21.Source = new Uri(url);
    }
           private  void webView21_DocumentCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
            {
                var url = webView21.CoreWebView2.Source;
                var task = Task.Run(() =>
                {
                    Thread.Sleep(2000); 
                });
                task.GetAwaiter().OnCompleted(async () =>
                {
                    var html = await webView21.CoreWebView2.ExecuteScriptAsync("document.getElementsByTagName('html')[0].outerHTML"); //第一次获取没法获取后端的数据
                     html = Unicode2String(html);
                     var obj = $"{{\"html\":{html}}}";
                     var b = JsonConvert.DeserializeObject<Doc>(obj);
                    File.WriteAllText("1.html", b.html);
                });
    }
            /// <summary>
            /// Unicode转字符串
            /// </summary>
            /// <param name="source">经过Unicode编码的字符串</param>
            /// <returns>正常字符串</returns>
             static string Unicode2String(string source)
            {
                return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(source,
                    x => Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)).ToString());
            }
  • 相关阅读:
    [Memcached]操作
    [Linux-CentOS7]安装Telnet
    PAT Advanced 1093 Count PAT's (25分)
    PAT Advanced 1065 A+B and C (64bit) (20分)
    PAT Advanced 1009 Product of Polynomials (25分)
    PAT Advanced 1008 Elevator (20分)
    PAT Advanced 1006 Sign In and Sign Out (25分)
    PAT Advanced 1002 A+B for Polynomials (25分)
    半年分布式处理回顾&机器学习(一)——线性回归
    PAT Advanced 1147 Heaps (30分)
  • 原文地址:https://www.cnblogs.com/simadi/p/15926861.html
Copyright © 2020-2023  润新知