C#数据采集用到的几个方法
这两天在做数据采集,因此整理了下数据采集要用到的一些方法。因为我采集的数据比较简单,所以没有用到框架。比较有名的两个框架 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;
} }
C#数据采集用到的几个方法的更多相关文章
- LabVIEW生成.NET的DLL——C#下调用NI数据采集设备功能的一种方法 [原创www.cnblogs.com/helesheng]
LabVIEW是NI公司的数据采集设备的标准平台,在其上调用NI-DAQmx驱动和接口函数能够高效的开发数据采集和控制程序.但作为一种图形化的开发语言,使用LabVIEW开发涉及算法和流程控制的大型应 ...
- 周报数据采集之生存图片(execl方法)
https://blog.csdn.net/Luzaofa/article/details/81675364 Python之Excel chart另存为图片大家好,好久没有更新博客了,这一段时间有点忙 ...
- 什么是Metrics-(通俗易懂)
在应用程序中,通常会记录日志以便事后分析,在很多情况下是产生了问题之后,再去查看日志,是一种事后的静态分析.在很多时候,我们可能需要了解整个系统在当前,或者某一时刻运行的情况,比如一个系统后台服务,我 ...
- 使用SNMP监控服务器运行情况
系统监测的基本概念及分类: a.系统监测的概述: 如何对现有IT架构的整体以及细节运行情况进行科学.系统和高效地监测是目前各企业运维和管理部门一项非常重要的工作内容.随着当前企业IT环境中服务器.应用 ...
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- Zabbix系统数据采集方法总结
转:http://www.blog.chinaunix.net/uid-9411004-id-4115731.html 老文章,直接拿来用了,官网也有最新分类,没高兴翻译 在Zabbix系统中有多达十 ...
- 卡尔曼(Kalman)滤波及十种数据采集滤波的方法和编程实例
卡尔曼(Kalman)滤波:https://blog.csdn.net/CSDN_X_W/article/details/90289021 十种数据采集滤波的方法和编程实例:https://wenku ...
- ETL数据采集方法
1.触发器方式 触发器方式是普遍采取的一种增量抽取机制.该方式是根据抽取要求,在要被抽取的源表上建立插入.修改.删除3个触发器,每当源表中的数据发生变化,就被相应的触发器将变化的数据写入一个增量日志表 ...
- 3种PHP实现数据采集的方法
https://www.php.cn/php-weizijiaocheng-387992.html
随机推荐
- go学习第三天、数据类型
基本数据类型 bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias ...
- 2018HDU多校训练-3-Problem M. Walking Plan
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331 Walking Plan Problem Description There are n inte ...
- 数理统计(一)——用Python进行方差分析
数理统计(一)——Python进行方差分析 iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 方差分析可以用来推断一个或多个因素在其状态变化时,其因素水平或交互 ...
- ARTS-S 获取子线程返回值注意事项
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...
- 【JS】395-重温基础:事件
本文是 重温基础 系列文章的第二十篇. 这是第三个基础系列的第一篇,欢迎持续关注呀!重温基础 系列的[初级]和[中级]的文章,已经统一整理到我的[Cute-JavaScript](http://js. ...
- 理解Vue中的ref和$refs
参考博客:https://www.cnblogs.com/xumqfaith/p/7743387.html
- NodeJS2-2环境&调试----引用系统内置模块,引用第三方模块
引用系统内置模块的方法 08_fs.js // 引用系统内置模块的方法 const fs = require('fs'); const result = fs.readFile('./08_fs.js ...
- IUSEP研修报告
目录 Introduction Alberta - Edmonton University of Alberta IUSEP Schoolwork and Project Principle of F ...
- OpenWrite一款博客可一文多发的实用工具
前言 许多网友想看一文多发的OpenWrite,今天,它来了!别问落地价,因为内测无价! 这款实用工具,可支持十大博客平台一键发布,是博主们的发文神器 你看它多种平台.一键管理.后台界面优雅.还有签到 ...
- uiautomatorviewer提示Unable to connect to adb. Check if adb is installed correctly解决方法
转自:https://www.jianshu.com/p/c8581a70d1bc 解决方案: 1.打开 "E:\android-sdk_r24.4.1-windows\android-s ...