• C#-提取网页中的超链接


    转载:http://www.wzsky.net/html/Program/net/26849.html
    using
    System; using System.Xml; using System.Text; using System.Net; using System.IO; using System.Collections; using System.Text.RegularExpressions; namespace test { class Program { static void Main(string[] args) { string strCode; ArrayList alLinks; Console.Write("请输入一个网页地址:"); string strURL = Console.ReadLine(); if (strURL.Substring(0, 7) != @"http://") { strURL = @"http://" + strURL; } Console.WriteLine("正在获取页面代码,请稍侯..."); strCode = GetPageSource(strURL); Console.WriteLine("正在提取超链接,请稍侯..."); alLinks = GetHyperLinks(strCode); Console.WriteLine("正在写入文件,请稍侯..."); WriteToXml(strURL, alLinks); } // 获取指定网页的HTML代码 static string GetPageSource(string URL) { Uri uri = new Uri(URL); HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri); HttpWebResponse hwRes = (HttpWebResponse)hwReq.GetResponse(); hwReq.Method = "Get"; hwReq.KeepAlive = false; StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312")); return reader.ReadToEnd(); } // 提取HTML代码中的网址 static ArrayList GetHyperLinks(string htmlCode) { ArrayList al = new ArrayList(); string strRegex = @"http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?"; Regex r = new Regex(strRegex, RegexOptions.IgnoreCase); MatchCollection m = r.Matches(htmlCode); for (int i = 0; i <= m.Count - 1; i++) { bool rep = false; string strNew = m[i].ToString(); // 过滤重复的URL foreach (string str in al) { if (strNew == str) { rep = true; break; } } if (!rep) al.Add(strNew); } al.Sort(); return al; } // 把网址写入xml文件 static void WriteToXml(string strURL, ArrayList alHyperLinks) { XmlTextWriter writer = new XmlTextWriter("HyperLinks.xml", Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(false); writer.WriteDocType("HyperLinks", null, "urls.dtd", null); writer.WriteComment("提取自" + strURL + "的超链接"); writer.WriteStartElement("HyperLinks"); writer.WriteStartElement("HyperLinks", null); writer.WriteAttributeString("DateTime", DateTime.Now.ToString()); foreach (string str in alHyperLinks) { string title = GetDomain(str); string body = str; writer.WriteElementString(title, null, body); } writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); writer.Close(); } // 获取网址的域名后缀 static string GetDomain(string strURL) { string retVal; string strRegex = @"(.com/|.net/|.cn/|.org/|.gov/)"; Regex r = new Regex(strRegex, RegexOptions.IgnoreCase); Match m = r.Match(strURL); retVal = m.ToString(); strRegex = @".|/$"; retVal = Regex.Replace(retVal, strRegex, "").ToString(); if (retVal == "") retVal = "other"; return retVal; } } }
  • 相关阅读:
    讲透学烂二叉树(五):分支平衡—AVL树与红黑树伸展树自平衡
    讲透学烂二叉树(四):二叉树的存储结构—建堆-搜索-排序
    讲透学烂二叉树(三):二叉树的遍历图解算法步骤及JS代码
    instanceof运算符的实质:Java继承链与JavaScript原型链
    JavaScript new 关键词解析及原生实现 new
    JavaScript继承的实现方式:原型语言对象继承对象原理剖析
    GitHub不再支持密码验证解决方案:SSH免密与Token登录配置
    PNG文件解读(2):PNG格式文件结构与数据结构解读—解码PNG数据
    PNG文件解读(1):PNG/APNG格式的前世今生
    JIT-动态编译与AOT-静态编译:java/ java/ JavaScript/Dart乱谈
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/3560980.html
Copyright © 2020-2023  润新知