2022年NET抓取和分析网页的类 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年NET抓取和分析网页的类 .pdf》由会员分享,可在线阅读,更多相关《2022年NET抓取和分析网页的类 .pdf(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、主要功能有:1、提取网页的纯文本,去所有html 标签和 javascript代码2、提取网页的链接,包括href 和 frame 及 iframe 3、提取网页的title等( 其它的标签可依此类推,正则是一样的) 4、可以实现简单的表单提交及cookie 保存第一部分using System; using System.Data; using System.Configuration; using System.Net; using System.IO; using System.Text; using System.Collections.Generic; using System.Te
2、xt.RegularExpressions; using System.Threading; using System.Web; / / 网页类/ public class WebPage #region 私有成员 private Uri m_uri; /网址 private List m_links; /此网页上的链接 private string m_title; /此网页的标题 private string m_html; /此网页的 HTML代码 private string m_outstr; /此网页可输出的纯文本 private bool m_good; /此网页是否可用 pri
3、vate int m_pagesize; /此网页的大小 private static Dictionary webcookies = new Dictionary();/存放所有网页的Cookie private string m_post; /此网页的登陆页需要的POST数据 private string m_loginurl; /此网页的登陆页 #endregion #region 私有方法 / / 这私有方法从网页的HTML 代码中分析出链接信息 / 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - -
4、- - - - - 第 1 页,共 15 页 - - - - - - - - - / List private List getLinks() if (m_links.Count = 0) Regex regex = new Regex2; regex0 = new Regex(?m)+href=(|)?(?(s)+)(|)?*(?(w|W)*?)/, RegexOptions.Multiline | RegexOptions.IgnoreCase); regex1 = new Regex(+src=(|)?(?(s)+)(|)?*, RegexOptions.Multiline | Rege
5、xOptions.IgnoreCase); for (int i = 0; i 2; i+) Match match = regexi.Match(m_html); while (match.Success) try string url = new Uri(m_uri, match.Groupsurl.Value).AbsoluteUri; string text = ; if (i = 0) text = new Regex(+)|(s)|( )|&|, RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(match
6、.Groupstext.Value, ); Link link = new Link(url, text); 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
7、 withLink) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 15 页 - - - - - - - - - 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
8、(?m)*(w|W)*?*, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Replace(m_outstr, ); m_outstr = new Regex(?m)*(w|W)*?*, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Replace(m_outstr, ); if (!withLink) m_outstr = new Regex(?m)*(w|W)*?*, RegexOptions.Multiline | RegexOptions.IgnoreCase).Replac
9、e(m_outstr, ); 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 = ob
10、jReg2.Replace(m_outstr, ); return m_outstr.Length firstN ? m_outstr.Substring(0, firstN) : m_outstr; / / 此私有方法返回一个IP 地址对应的无符号整数 / / IP地址 / private uint getuintFromIP(IPAddress x) Byte bt = x.GetAddressBytes(); uint i = (uint)(bt0 * 256 * 256 * 256); i += (uint)(bt1 * 256 * 256); i += (uint)(bt2 * 25
11、6); i += (uint)(bt3); return i; #endregion #region 公有文法 / / 此公有方法提取网页中一定字数的纯文本,包括链接文字 / / 字数 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 15 页 - - - - - - - - - / public string getContext(int firstN) return getFirstNchar(m_html, firstN, true); / / 此公有方法提取网页中一
12、定字数的纯文本,不包括链接文字 / / / public string getContextWithOutLink(int firstN) return getFirstNchar(m_html, firstN, false); / / 此公有方法从本网页的链接中提取一定数量的链接,该链接的URL满足某正则式 / / 正则式 / 返回的链接的个数 / List public List getSpecialLinksByUrl(string pattern,int count) if(m_links.Count=0)getLinks(); List SpecialLinks = new List
13、(); List.Enumerator i; i = m_links.GetEnumerator(); int cnt = 0; while (i.MoveNext() & cntcount) if (new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.url).Success) SpecialLinks.Add(i.Current); cnt+; return SpecialLinks; / / 此公有方法从本网页的链接中提取一定数量的链接,该链接的文字满足某正则式名师资料
14、总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 15 页 - - - - - - - - - / / 正则式 / 返回的链接的个数 / List public List getSpecialLinksByText(string pattern,int count) if (m_links.Count = 0) getLinks(); List SpecialLinks = new List(); List.Enumerator i; i = m_links.GetEnumerat
15、or(); int cnt = 0; while (i.MoveNext() & cnt count) if (new Regex(pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase ).Match(i.Current.text).Success) SpecialLinks.Add(i.Current); cnt+; return SpecialLinks; / / 此公有方法获得所有链接中在一定IP 范围的链接 / / 起始 IP / 终止 IP / public List getSpecialLinksByIP(string
16、_ip_start, string _ip_end) IPAddress ip_start = IPAddress.Parse(_ip_start); IPAddress ip_end = IPAddress.Parse(_ip_end); if (m_links.Count = 0) getLinks(); List SpecialLinks = new List(); List.Enumerator i; i = m_links.GetEnumerator(); while (i.MoveNext() IPAddress ip; try ip = Dns.GetHostEntry(new
17、Uri(i.Current.url).Host).AddressList0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 15 页 - - - - - - - - - catch continue; if(getuintFromIP(ip)=getuintFromIP(ip_start) & getuintFromIP(ip)=getuintFromIP(ip_end) SpecialLinks.Add(i.Current); return SpecialLinks;
18、 / / 这公有方法提取本网页的纯文本中满足某正则式的文字 / / 正则式 / 返回文字 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.Groups1.Value; return
19、 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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - -
20、 - - - - - 名师精心整理 - - - - - - - 第 6 页,共 15 页 - - - - - - - - - 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; rq
21、st.Timeout = 30000; lock (WebPage.webcookies) if (WebPage.webcookies.ContainsKey(m_uri.Host) rqst.CookieContainer = WebPage.webcookiesm_uri.Host; else CookieContainer cc = new CookieContainer(); WebPage.webcookiesm_uri.Host = cc; rqst.CookieContainer = cc; HttpWebResponse rsps = (HttpWebResponse)rqs
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年NET抓取和分析网页的类 2022 NET 抓取 分析 网页
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内