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()); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2021-02-23 wpf 复制到粘贴板_WPF Clipboard剪贴板缺陷解决方案
2016-02-23 浏览器被劫持到http://hao.169x.cn/?v=108的解决办法