C#获取html标签内容的方法
2024-09-08 14:11:14
C# 获取html标签内容的方法:
/// <summary>
/// 获取html网页标签内容
/// 例如:<span class="index_infoItem__ESU0o"></span>
/// </summary>
/// <param name="html">html内容</param>
/// <param name="tag">标签 例如:span</param>
/// <param name="attribute">标签属性 例如:class</param>
/// <param name="value">标签属性值 例如:index_infoItem__ESU0o</param>
/// <returns></returns>
public static string[] RegexHtmlToFormat(string html, string tag, string attribute, string value)
{
List<string> list = new List<string>();
string regex_html = @"<"+ tag + ".*?"+ attribute + "=.*?"+ value + ".*?[^>]*?>.*?</" + tag + ">"; //定义html标签的正则表达式
Regex regex = new Regex(regex_html, RegexOptions.IgnoreCase);
if (regex.IsMatch(html))
{
MatchCollection matchCollection = regex.Matches(html);
foreach (Match match in matchCollection)
{
var valueHtml = match.Value;
valueHtml = Regex.Replace(valueHtml, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);//去除html标签
list.Add(valueHtml);//获取到的
}
}
return list.ToArray();
}
最新文章
- 【Swift学习】Swift编程之旅---继承(十七)
- charles使用教程指南(抓包工具)
- ecshop后台通过ajax搜索原理
- 《TCP/IP具体解释卷2:实现》笔记--IP多播
- [MVC] - 异步调用后台的常用方法。
- update语句
- jQuery编写插件--封装全局函数的插件(一些常用的js验证表达式)
- 修改Calendar(梅花雨)日历控件 兼容IE9 谷歌 火狐
- C/C++各种类型int、long、double、char表示范围(最大和最小)
- X-NUCA 2017 web专题赛训练题 阳光总在风雨后和default wp
- Bash shell命令记录和CentOS的一些技巧
- JMeter监控服务器CPU、内存的方法
- Python学习之路基础篇--07Python基础+编码、集合 和 深浅Copy
- Android 指定 Theme
- 05. .stop、.prevent、.capture、.self、.once、
- ODBC 驱动程序管理器 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配 解决方案
- Linux学习笔记07—mysql的配置
- 迷你MVVM框架 avalonjs 1.3.9发布
- c++之单例模式
- ACM训练大纲
热门文章
- JUC学习笔记——共享模型之不可变
- SpringBoot+hutool工具-数据库数据导出Excel
- 解决Linux使用deepin-wine跑qq,tim打不开文件所在文件夹
- python-面向对象属性的访问与self的理解
- day02 数据类型 &; 运算符
- 【Shell案例】【for循环、seq生成】3、输出7的倍数
- 笔试面试--Java基础知识
- Spring学习笔记 - 第二章 - 注解开发、配置管理第三方Bean、注解管理第三方Bean、Spring 整合 MyBatis 和 Junit 案例
- 布尔值、元组、集合、input、格式化输出、赋值、运算符
- 动态更改Spring定时任务Cron表达式的优雅方案