在本教程中,我们将展示如何使用 C# 和 WinForms 创建一个简单的工具,抓取微信小程序的资源和微信视频号的视频内容。此工具可以帮助开发者和研究人员分析和获取微信小程序和视频号中的有用数据。
预备知识
- 基本的 C# 编程知识。
- 基本的 WinForms 开发知识。
- 对网络请求和数据抓取的基本了解。
环境准备
- 安装 .NET 8 SDK。
- 安装 Visual Studio 2022 或 Visual Studio Code。
创建项目
- 打开 Visual Studio,创建一个新的 Windows Forms 应用程序项目。
- 在“创建新项目”窗口中,选择“Windows Forms App (.NET Core)”,然后点击“下一步”。
- 设置项目名称和保存位置,然后点击“创建”。
安装必要的库
为了发送和处理 HTTP 请求,我们将使用 HttpClient
类。此外,我们将使用 HtmlAgilityPack
库来解析 HTML 内容。
打开 NuGet 包管理器控制台,运行以下命令来安装 HtmlAgilityPack
:
Install-Package HtmlAgilityPack
编写代码
在 Form1.Designer.cs 文件中设计一个简单的用户界面,包括一个按钮用于开始抓取,一个文本框用于输入微信小程序或视频号链接,一个列表框用于显示抓取的资源。
this.startButton = new System.Windows.Forms.Button();
this.urlTextBox = new System.Windows.Forms.TextBox();
this.resourcesListBox = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// startButton
//
this.startButton.Location = new System.Drawing.Point(12, 12);
this.startButton.Name = "startButton";
this.startButton.Size = new System.Drawing.Size(75, 23);
this.startButton.TabIndex = 0;
this.startButton.Text = "开始抓取";
this.startButton.UseVisualStyleBackColor = true;
this.startButton.Click += new System.EventHandler(this.StartButton_Click);
//
// urlTextBox
//
this.urlTextBox.Location = new System.Drawing.Point(12, 41);
this.urlTextBox.Name = "urlTextBox";
this.urlTextBox.Size = new System.Drawing.Size(776, 20);
this.urlTextBox.TabIndex = 1;
//
// resourcesListBox
//
this.resourcesListBox.FormattingEnabled = true;
this.resourcesListBox.Location = new System.Drawing.Point(12, 67);
this.resourcesListBox.Name = "resourcesListBox";
this.resourcesListBox.Size = new System.Drawing.Size(776, 368);
this.resourcesListBox.TabIndex = 2;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.resourcesListBox);
this.Controls.Add(this.urlTextBox);
this.Controls.Add(this.startButton);
this.Name = "Form1";
this.Text = "微信抓取工具";
this.ResumeLayout(false);
this.PerformLayout();
在 Form1.cs 文件中添加以下代码:
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace WeChatScraper
{
public partial class Form1 : Form
{
private readonly HttpClient _httpClient;
public Form1()
{
InitializeComponent();
_httpClient = new HttpClient();
}
private async void StartButton_Click(object sender, EventArgs e)
{
string url = urlTextBox.Text;
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("请输入一个有效的URL");
return;
}
await ScrapeWeChatContent(url);
}
private async Task ScrapeWeChatContent(string url)
{
try
{
var response = await _httpClient.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(response);
var resourceNodes = htmlDocument.DocumentNode.SelectNodes("//img | //video");
resourcesListBox.Items.Clear();
if (resourceNodes != null)
{
foreach (var node in resourceNodes)
{
string resourceUrl = node.GetAttributeValue("src", string.Empty);
if (!string.IsNullOrEmpty(resourceUrl))
{
resourcesListBox.Items.Add(resourceUrl);
}
}
}
else
{
MessageBox.Show("未找到资源。");
}
}
catch (Exception ex)
{
MessageBox.Show($"抓取过程中出现错误: {ex.Message}");
}
}
}
}
运行应用程序
- 编译并运行你的应用程序。
- 在文本框中输入微信小程序或者视频号的链接。
- 点击“开始抓取”按钮,工具会抓取并显示页面中的图片和视频资源链接。