php正则函数抓取网页连接(本文实现Excel表数据导入SqlServer中的方法)

优采云 发布时间: 2021-11-16 21:17

  php正则函数抓取网页连接(本文实现Excel表数据导入SqlServer中的方法)

  本文介绍了基于正则表达式的C#网页爬取类获取网页中所有信息的示例。分享给大家,供大家参考,如下:

  类的代码:

<p>

using System;

using System.Data;

using System.Configuration;

using System.Net;

using System.IO;

using System.Text;

using System.Collections.Generic;

using System.Text.RegularExpressions;

using System.Threading;

using System.Web;

using System.Web.UI.MobileControls;

///

/// 网页类

///

public class WebPage

{

#region 私有成员

private Uri m_uri; //url

private List m_links; //此网页上的链接

private string m_title; //标题

private string m_html; //HTML代码

private string m_outstr; //网页可输出的纯文本

private bool m_good; //网页是否可用

private int m_pagesize; //网页的大小

private static Dictionary webcookies = new Dictionary();//存放所有网页的Cookie

#endregion

#region 属性

///

/// 通过此属性可获得本网页的网址,只读

///

public string URL

{

get

{

return m_uri.AbsoluteUri;

}

}

///

/// 通过此属性可获得本网页的标题,只读

///

public string Title

{

get

{

if (m_title == "")

{

Regex reg = new Regex(@"(?m)]*>(?(?:\w|\W)*?)]*>",RegexOptions.Multiline | RegexOptions.IgnoreCase);

Match mc = reg.Match(m_html);

if (mc.Success)

m_title = mc.Groups["title"].Value.Trim();

}

return m_title;

}

}

public string M_html

{

get

{

if (m_html == null)

{

m_html = "";

}

return m_html;

}

}

///

/// 此属性获得本网页的所有链接信息,只读

///

public List Links

{

get

{

if (m_links.Count == 0) getLinks();

return m_links;

}

}

///

/// 此属性返回本网页的全部纯文本信息,只读

///

public string Context

{

get

{

if (m_outstr == "") getContext(Int16.MaxValue);

return m_outstr;

}

}

///

/// 此属性获得本网页的大小

///

public int PageSize

{

get

{

return m_pagesize;

}

}

///

/// 此属性获得本网页的所有站内链接

///

public List InsiteLinks

{

get

{

return getSpecialLinksByUrl("^http://" + m_uri.Host,Int16.MaxValue);

}

}

///

/// 此属性表示本网页是否可用

///

public bool IsGood

{

get

{

return m_good;

}

}

///

/// 此属性表示网页的所在的网站

///

public string Host

{

get

{

return m_uri.Host;

}

}

#endregion

///

/// 从HTML代码中分析出链接信息

///

/// List

private List getLinks()

{

if (m_links.Count == 0)

{

Regex[] regex = new Regex[2];

regex[0] = new Regex(@"(?[^]*>",RegexOptions.IgnoreCase);

for (int i = 0; i < 2; i++)

{

Match match = regex[i].Match(m_html);

while (match.Success)

{

try

{

string url = HttpUtility.UrlDecode(new Uri(m_uri,match.Groups["URL"].Value).AbsoluteUri);

string text = "";

if (i == 0) text = new Regex("(]+>)|(\\s)|( )|&|\"",RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(match.Groups["text"].Value,"");

Link link = new Link();

link.Text = text;

link.NavigateUrl = url;

m_links.Add(link);

}

catch (Exception ex) { Console.WriteLine(ex.Message); };

match = match.Nextmatch();

}

}

}

return m_links;

}

///

/// 此私有方法从一段HTML文本中提取出一定字数的纯文本

///

/// HTML代码

/// 提取从头数多少个字

/// 是否要链接里面的字

/// 纯文本

private string getFirstNchar(string instr,int firstN,bool withLink)

{

if (m_outstr == "")

{

m_outstr = instr.Clone() as string;

m_outstr = new Regex(@"(?m)]*>(\w|\W)*?]*>",RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr,"");

m_outstr = new Regex(@"(?m)]*>(\w|\W)*?]*>","");

m_outstr = new Regex(@"(?m)]*>(\w|\W)*?]*>","");

if (!withLink) m_outstr = new Regex(@"(?m)]*>(\w|\W)*?]*>","");

Regex objReg = new System.Text.RegularExpressions.Regex("(]+?>)| ",RegexOptions.Multiline | RegexOptions.IgnoreCase);

m_outstr = objReg.Replace(m_outstr,"");

Regex objReg2 = new System.Text.RegularExpressions.Regex("(\\s)+",RegexOptions.Multiline | RegexOptions.IgnoreCase);

m_outstr = objReg2.Replace(m_outstr," ");

}

return m_outstr.Length > firstN ? m_outstr.Substring(0,firstN) : m_outstr;

}

#region 公有文法

///

/// 此公有方法提取网页中一定字数的纯文本,包括链接文字

///

/// 字数

///

public string getContext(int firstN)

{

return getFirstNchar(m_html,firstN,true);

}

///

/// 此公有方法从本网页的链接中提取一定数量的链接,该链接的URL满足某正则式

///

/// 正则式

/// 返回的链接的个数

/// List

public List getSpecialLinksByUrl(string pattern,int count)

{

if (m_links.Count == 0) getLinks();

List SpecialLinks = new List();

List.Enumerator i;

i = m_links.GetEnumerator();

int cnt = 0;

while (i.MoveNext() && cnt < count)

{

if (new Regex(pattern,RegexOptions.Multiline | RegexOptions.IgnoreCase).Match(i.Current.NavigateUrl).Success)

{

SpecialLinks.Add(i.Current);

cnt++;

}

}

return SpecialLinks;

}

///

/// 此公有方法从本网页的链接中提取一定数量的链接,该链接的文字满足某正则式

///

/// 正则式

/// 返回的链接的个数

/// List

public List getSpecialLinksByText(string pattern,RegexOptions.Multiline | RegexOptions.IgnoreCase).Match(i.Current.Text).Success)

{

SpecialLinks.Add(i.Current);

cnt++;

}

}

return SpecialLinks;

}

///

/// 这公有方法提取本网页的纯文本中满足某正则式的文字 by 何问起

///

/// 正则式

/// 返回文字

public string getSpecialWords(string pattern)

{

if (m_outstr == "") getContext(Int16.MaxValue);

Regex regex = new Regex(pattern,RegexOptions.Multiline | RegexOptions.IgnoreCase);

Match mc = regex.Match(m_outstr);

if (mc.Success)

return mc.Groups[1].Value;

return string.Empty;

}

#endregion

#region 构造函数

private void Init(string _url)

{

try

{

m_uri = new Uri(_url);

m_links = new List();

m_html = "";

m_outstr = "";

m_title = "";

m_good = true;

if (_url.EndsWith(".rar") || _url.EndsWith(".dat") || _url.EndsWith(".msi"))

{

m_good = false;

return;

}

HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create(m_uri);

rqst.AllowAutoRedirect = true;

rqst.MaximumAutomaticRedirections = 3;

rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

rqst.KeepAlive = true;

rqst.Timeout = 10000;

lock (WebPage.webcookies)

{

if (WebPage.webcookies.ContainsKey(m_uri.Host))

rqst.CookieContainer = WebPage.webcookies[m_uri.Host];

else

{

CookieContainer cc = new CookieContainer();

WebPage.webcookies[m_uri.Host] = cc;

rqst.CookieContainer = cc;

}

}

HttpWebResponse rsps = (HttpWebResponse)rqst.GetResponse();

Stream sm = rsps.GetResponseStream();

if (!rsps.ContentType.ToLower().StartsWith("text/") || rsps.ContentLength > 1

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线