这两天在做数据采集,因此整理了下数据采集要用到的一些方法。因为我采集的数据比较简单,所以没有用到框架。比较有名的两个框架 HtmlAgilityPack 和 Jumony,感兴趣的可以研究下。当然,火车头采集工具也很方便,不过要付费。下面是整理的代码:

   /// <summary>
/// Html正则处理帮助类
/// </summary>
public class HtmlRegex
{
/// <summary>
/// 匹配所有Html标签
/// </summary>
const string HTMLALLTAG = @"<[^>]+>|</[^>]+>"; /// <summary>
/// 删除所有html标签
/// </summary>
/// <param name="content">原HTML代码</param>
/// <returns></returns>
public static string RemoveAllHtml(string content)
{
return Regex.Replace(content, HTMLALLTAG, "");
} /// <summary>
/// 根据正则匹配获取指定内容
/// </summary>
/// <param name="regStr">正则</param>
/// <param name="content">原HTML代码</param>
/// <param name="hashtml">是否包含HTML标签</param>
/// <returns></returns>
public static string GetStrByRegex(string regStr, string content, bool hashtml = true)
{
string result = string.Empty;
Regex reg = new Regex(regStr);
Match mth = reg.Match(content); if (mth.Success)
{
result = mth.Value;
if (!hashtml) result = HtmlRegex.RemoveAllHtml(result); //去除html标签 }
return result;
} /// <summary>
/// 获取指定位置的html代码
/// </summary>
/// <param name="start">起始字符串</param>
/// <param name="end">结束字符串</param>
/// <param name="content">原HTML代码</param>
/// <param name="hasHtml">是否包含HTML标签</param>
/// <returns></returns>
public static string GetStrByRegex(string start, string end, string content, bool hasHtml = true)
{
string result = string.Empty;
string regStr = @"(?is)(" + start + ").*?(" + end + ")";
Regex reg = new Regex(regStr);
Match mth = reg.Match(content);
if (mth.Success)
{
result = mth.Value;
if (!hasHtml) result = HtmlRegex.RemoveAllHtml(result); //去除html标签
}
return result;
} /// <summary>
/// 获取匹配的字符串列表
/// </summary>
/// <param name="regStr">正则</param>
/// <param name="content">原HTML代码</param>
/// <returns></returns>
public static List<string> GetStrListByRegex(string regStr, string content)
{
List<string> strList = null;
MatchCollection mc = null;
try
{
Regex reg = new Regex(regStr);
mc = reg.Matches(content);
if (mc.Count > )
{
strList = new List<string>();
for (int i = ; i < mc.Count; i++)
{
strList.Add(mc[i].Value);
}
}
}
catch
{
strList = null;
}
return strList;
} /// <summary>
/// 获取匹配的字符串列表
/// </summary>
/// <param name="start">起始字符串</param>
/// <param name="end">结束字符串</param>
/// <param name="content">原HTML代码</param>
/// <returns></returns>
public static List<string> GetStrListByRegex(string start, string end, string content)
{
List<string> strList = null;
MatchCollection mc = null;
string regStr = @"(?is)(" + start + ").*?(" + end + ")";
try
{
Regex reg = new Regex(regStr);
mc = reg.Matches(content);
if (mc.Count > )
{
strList = new List<string>();
for (int i = ; i < mc.Count; i++)
{
strList.Add(mc[i].Value);
}
}
}
catch
{
strList = null;
}
return strList;
} }

最新文章

  1. HDFS DataNode 设计实现解析
  2. sqlalchemy默认时间
  3. Hive的API的说明
  4. 19.dnw打不开
  5. SPARQL1.1 101 Language and Jena support
  6. Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
  7. 2016.04.09 使用Powerdesigner进行创建数据库的概念模型并转为物理模型
  8. 了解一下jsp
  9. android gradle 多渠道打包
  10. WPF案例 (三) 模拟QQ“快速换装&quot;界面
  11. Windows Phone开发(23):启动器与选择器之CameraCaptureTask和PhotoChooserTask
  12. lua 中操作系统库
  13. Jquery跨域读取城市天气预报信息
  14. CentOS 7修改网卡名称
  15. mybatis基础(下)
  16. Python进程池Pool
  17. tcp,Socket,三次握手和四次挥手的图示
  18. CSAPP:信息的表和处理1
  19. JDK1.8源码逐字逐句带你理解LinkedHashMap底层
  20. 数据结构与算法--最小生成树之Kruskal算法

热门文章

  1. 创建自己的github仓库
  2. HDU1517 Multiply Game
  3. CSU OJ2151 集训难度
  4. python 金融应用(四)金融时间序列分析基础
  5. ARTS-S anaconda常用命令
  6. Java并发:线程限制
  7. 【Vuejs】301- Vue 3.0前的 TypeScript 最佳入门实践
  8. 【系列专题】JavaScript 重温系列(22篇全)
  9. 快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
  10. Scrcpy用电脑控制Android手机(支持Windows/macOS/Linux)