• 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());
            }
  • 相关阅读:
    ASP.NET程序中常用的三十三种代码
    uri
    用XML保存和读取图片
    location
    访问相关节点
    onkeyup,onkeydown,onkeypress
    Asp.net中Server.Transfer,Server.Execute和Response.Redirect的区别
    关于window.showModalDialog()返回值的学习心得
    WP7数据绑定
    hdu 1568
  • 原文地址:https://www.cnblogs.com/simadi/p/15926861.html
Copyright © 2020-2023  润新知