[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using JinYiHelp.StringHelp;
namespace 查单词
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button_搜索_Click(object sender, EventArgs e)
{
if (textBox_单词.Text == "") {
MessageBox.Show("搜索内容不能为空!");
return;
}
string apiUrl = "https://www.iciba.com/word?w="+textBox_单词.Text;
HttpClient http = new HttpClient();
HttpResponseMessage resq = await http.GetAsync(apiUrl);
if (!resq.IsSuccessStatusCode)
{
Debug.WriteLine($"Error: {resq.StatusCode}");
return;
}
string htmlContent = await resq.Content.ReadAsStringAsync();
string left = @"<ul class=""Mean_part__UI9M6"">";
string right = @"</ul>";
string mean = StringHelper.Between(htmlContent, left, right);
string[] liTexts = ExtractLiTexts(mean);
textBox_结果.Text = "";
foreach (string li in liTexts)
{
//Debug.WriteLine(RemoveHtmlTags(li));
string w = RemoveHtmlTags(li);
textBox_结果.AppendText(w + Environment.NewLine);
}
}
static bool IsChinese(string str)
{
// 汉字的 Unicode 范围
string pattern = @"[\u4e00-\u9fff]";
return Regex.IsMatch(str, pattern);
}
static string RemoveHtmlTags(string input)
{
return Regex.Replace(input, "<.*?>", "");
}
static string[] ExtractLiTexts(string html)
{
string pattern = @"<li>(.*?)</li>";
MatchCollection matches = Regex.Matches(html, pattern);
string[] liTexts = new string[matches.Count];
for (int i = 0; i < matches.Count; i++)
{
liTexts = matches.Groups[1].Value.Trim();
}
return liTexts;
}
}
}
|