zoukankan
html css js c++ java
提取网页中的链接并生成xml
using System; using System.Xml; using System.Text; using System.Net; using System.IO; using System.Collections; using System.Text.RegularExpressions; public class App { public static void Main() { 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 = @"\.|/{1}quot;; retVal = Regex.Replace(retVal, strRegex, "").ToString(); if (retVal == "") retVal = "other"; return retVal; } }
查看全文
相关阅读:
定位IO瓶颈的方法,iowait低,IO就没有到瓶颈?
10分钟检查自己的系统性能数据
netperf使用指南
如何看内核源码
xxx
os.path 模块
目前中国智能语音产业的格局、现状
NLP-python 自然语言处理01
15本经典金融投资著作
写给步入工作的自己
原文地址:https://www.cnblogs.com/zhangqs008/p/2341121.html
最新文章
c++中 endl的意思?
c++ question 003 求两数大者?
c++程序设计第三版例题1.2 求两数的和
生活 感觉
oracle number 类型 只显示10位精度
timezone
定时器
单片机系统的时间
模拟器件之晶体三极管
电源芯片:
热门文章
模拟器件之三极管场效应管
模拟器件之二极管
模拟电路巩固笔记
HAL之串口
HAL之PWM
HAL之定时器
/proc/sys/vm man手册
内核内存碎片管理
理解Linux CPU负载和 CPU使用率
[转载]硬盘监控和分析工具:Smartctl
Copyright © 2011-2022 走看看