今天给大家列出一些代码,仅供参考
列出数据层和逻辑层的代码
列出数据层和逻辑层的代码
WebPage类
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Web;
5using System.Web.SessionState;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.HtmlControls;
9namespace WebPage
10{
11 public class StringHelper
12 {
13 public StringHelper()
14 {
15 }
16 /**//// <summary>
17 /// 将中文字符转换为URL编码格式
18 /// </summary>
19 /// <param name="strText">字符串</param>
20 /// <returns>URL编码格式</returns>
21 static public string GetChineseURLCode(string strText)
22 {
23 int len = strText.Length;
24 string myStr = "";
25 for (int i = 0; i < len; i++)
26 {
27 myStr += getSpell(strText.Substring(i, 1));
28 }
29 return myStr;
30 }
31
32 static private string getSpell(string cnChar)
33 {
34 byte[] arrCN = System.Text.Encoding.Default.GetBytes(cnChar);
35 if (arrCN.Length > 1)
36 {
37 return System.Web.HttpUtility.UrlEncode(cnChar);
38 }
39 else
40 {
41 return cnChar;
42 }
43 }
44 }
45}
46
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Web;
5using System.Web.SessionState;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.HtmlControls;
9namespace WebPage
10{
11 public class StringHelper
12 {
13 public StringHelper()
14 {
15 }
16 /**//// <summary>
17 /// 将中文字符转换为URL编码格式
18 /// </summary>
19 /// <param name="strText">字符串</param>
20 /// <returns>URL编码格式</returns>
21 static public string GetChineseURLCode(string strText)
22 {
23 int len = strText.Length;
24 string myStr = "";
25 for (int i = 0; i < len; i++)
26 {
27 myStr += getSpell(strText.Substring(i, 1));
28 }
29 return myStr;
30 }
31
32 static private string getSpell(string cnChar)
33 {
34 byte[] arrCN = System.Text.Encoding.Default.GetBytes(cnChar);
35 if (arrCN.Length > 1)
36 {
37 return System.Web.HttpUtility.UrlEncode(cnChar);
38 }
39 else
40 {
41 return cnChar;
42 }
43 }
44 }
45}
46
这个是逻辑层的一个辅助类
WebDetail类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Text.RegularExpressions;
/**//// <summary>
/// 功能:文章最终页类
/// 创建时间:07-3-5
/// 创建人:曹振华
/// </summary>
namespace WebPage
{
//文章最终页类
public class WebDetailPage:WebPage
{
private string _strDelRegex;
public WebDetailPage(WebDetailPageConfig clsDetailPage)
{
Url=clsDetailPage.DetailUrl;
CutRegex=clsDetailPage.strCutRegex;
TimeOut=clsDetailPage.intTimeOut;
filterFlag = clsDetailPage.filterFlag;
_strDelRegex = clsDetailPage.strDelRegex;
}
//获得文章最终页截取的内容
public string GetDetailPageHtml()
{
string strHtml = "";
Match match = matchRegexHtml;
if (match.Success)
{
strHtml = match.Groups["tmpDetailContent"].Value.ToString().Trim();
strHtml = Regex.Replace(strHtml, _strDelRegex, "");
}
return strHtml;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Text.RegularExpressions;
/**//// <summary>
/// 功能:文章最终页类
/// 创建时间:07-3-5
/// 创建人:曹振华
/// </summary>
namespace WebPage
{
//文章最终页类
public class WebDetailPage:WebPage
{
private string _strDelRegex;
public WebDetailPage(WebDetailPageConfig clsDetailPage)
{
Url=clsDetailPage.DetailUrl;
CutRegex=clsDetailPage.strCutRegex;
TimeOut=clsDetailPage.intTimeOut;
filterFlag = clsDetailPage.filterFlag;
_strDelRegex = clsDetailPage.strDelRegex;
}
//获得文章最终页截取的内容
public string GetDetailPageHtml()
{
string strHtml = "";
Match match = matchRegexHtml;
if (match.Success)
{
strHtml = match.Groups["tmpDetailContent"].Value.ToString().Trim();
strHtml = Regex.Replace(strHtml, _strDelRegex, "");
}
return strHtml;
}
}
}
这个是个页面最终解析类
WebPage类
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace WebPage
{
/**//// <summary>
/// 功能:web页面基类
/// 创建时间:07-3-5
/// 创建人:曹振华
/// </summary>
public class WebPage
{
private string _strUrl;
private int _intTimeOut;//设置抓取时间
private string _strCutHtmlRegex;//截取列表内容部分正则
private int _intflag;
//网页的url地址
public string Url
{
get { return _strUrl; }
set { _strUrl = value; }
}
//抓取超时时间
public int TimeOut
{
get { return _intTimeOut; }
set { _intTimeOut = value; }
}
//截取主要内容的正则表达式
public string CutRegex
{
get { return _strCutHtmlRegex; }
set { _strCutHtmlRegex = value; }
}
//网页的内容
public string PageHtml
{
get { return GetUrlstrHtml(); }
}
//获取正则表达式得到的match
public Match matchRegexHtml
{
get { return GetRegexHtml(); }
}
//过滤标志
public int filterFlag
{
get { return _intflag; }
set { _intflag = value; }
}
/**////////////////////////////////////////////////////////////////////////////////////////////
//获取页面内容
protected string GetUrlstrHtml()
{
string strHtml = "";
strHtml = WebPageTools.GetConent(_strUrl, _intTimeOut, filterFlag);
return strHtml;
}
//获取截取的页面内容
protected Match GetRegexHtml()
{
Regex reg = new Regex(_strCutHtmlRegex, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
Match match;
try
{
match = reg.Match(GetUrlstrHtml());
}
catch
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace WebPage
{
/**//// <summary>
/// 功能:web页面基类
/// 创建时间:07-3-5
/// 创建人:曹振华
/// </summary>
public class WebPage
{
private string _strUrl;
private int _intTimeOut;//设置抓取时间
private string _strCutHtmlRegex;//截取列表内容部分正则
private int _intflag;
//网页的url地址
public string Url
{
get { return _strUrl; }
set { _strUrl = value; }
}
//抓取超时时间
public int TimeOut
{
get { return _intTimeOut; }
set { _intTimeOut = value; }
}
//截取主要内容的正则表达式
public string CutRegex
{
get { return _strCutHtmlRegex; }
set { _strCutHtmlRegex = value; }
}
//网页的内容
public string PageHtml
{
get { return GetUrlstrHtml(); }
}
//获取正则表达式得到的match
public Match matchRegexHtml
{
get { return GetRegexHtml(); }
}
//过滤标志
public int filterFlag
{
get { return _intflag; }
set { _intflag = value; }
}
/**////////////////////////////////////////////////////////////////////////////////////////////
//获取页面内容
protected string GetUrlstrHtml()
{
string strHtml = "";
strHtml = WebPageTools.GetConent(_strUrl, _intTimeOut, filterFlag);
return strHtml;
}
//获取截取的页面内容
protected Match GetRegexHtml()
{
Regex reg = new Regex(_strCutHtmlRegex, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
Match match;
try
{
match = reg.Match(GetUrlstrHtml());
}
catch
作者: zhaoxiangan
- 该日志由 zhaoxiangan 于12年前发表在综合分类下,最后更新于 2012年07月07日.
- 转载请注明: 爬虫/蜘蛛程序的制作(C#语言) 二 | 学步园 +复制链接
抱歉!评论已关闭.