转载: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(, ) != @"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 = ; i <= m.Count - ; 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;
}
}
}

C#-提取网页中的超链接的更多相关文章

  1. python笔记之提取网页中的超链接

    python笔记之提取网页中的超链接 对于提取网页中的超链接,先把网页内容读取出来,然后用beautifulsoup来解析是比较方便的.但是我发现一个问题,如果直接提取a标签的href,就会包含jav ...

  2. 用html.parser抓网页中的超链接,返回list

    #python3 from html.parser import HTMLParser class MyHTMLParser(HTMLParser): """ 1.tag ...

  3. python学习笔记——爬虫中提取网页中的信息

    1 数据类型 网页中的数据类型可分为结构化数据.半结构化数据.非结构化数据三种 1.1 结构化数据 常见的是MySQL,表现为二维形式的数据 1.2 半结构化数据 是结构化数据的一种形式,并不符合关系 ...

  4. [爬虫学习笔记]用于提取网页中所有链接的 Extractor 模块

            Extractor的工作是从下载的网页中将它包含的所有URL提取出来.这是个细致的工作,你需要考虑到所有可能的url的样式,比如网页中常常会包含相对路径的url,提取的时候需要将它转换 ...

  5. 网页中的超链接<a>标签

    格式: <a href="目标网址" title="鼠标滑过显示的文本">链接显示的文本</a> 注意:为文本加入<a>标签 ...

  6. C#正则表达式通过HTML提取网页中的图片src

    目前在做HoverTreeCMS项目中有处理图片的部分,参考了一下网上案例,自己写了一个获取内容中的图片地址的方法. 可以先看看效果:http://tool.hovertree.com/a/zz/im ...

  7. 【搜索引擎Jediael开发笔记3】使用HtmlParser提取网页中的链接

    关于HtmpParser的基本内容请见 HtmlParser基础教程 本文示例用于提取HTML文件中的链接 package org.ljh.search.html; import java.util. ...

  8. 【google chrome 一键打开 谷歌跳转的页面+JS Replace】谷歌无法打开网页的时候,提取网页中url的部分

    经常在谷歌搜索,遇到网页无法打开,然后就停留在比如:http://www.google.com.hk/search?newwindow=1&safe=strict&site=& ...

  9. Python:提取网页中的电子邮箱

    import requests, re #regex = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"#这个正则表达式过滤 ...

随机推荐

  1. post&get请求总结

    1.将get获取的数据,UrlDecode后返回 public static string SendGet(string url) { HttpWebRequest httpWebRequest = ...

  2. 【Avalon】获取隐藏元素的尺寸

    保存原来的属性 设置成显示的属性 获取尺寸 设置回原来的属性 var cssShow = { position: "absolute", visibility: "hid ...

  3. Activity Lifecycle

    Activity Lifecycle Activities in the system are managed as an activity stack(activity栈?). When a new ...

  4. PAT (Basic Level) Practise:1017. A除以B

    [题目链接] 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格 ...

  5. 转载:奇异值分解(SVD) --- 线性变换几何意义(下)

    本文转载自他人: PS:一直以来对SVD分解似懂非懂,此文为译文,原文以细致的分析+大量的可视化图形演示了SVD的几何意义.能在有限的篇幅把这个问题讲解的如此清晰,实属不易.原文举了一个简单的图像处理 ...

  6. 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  7. C++ Primer : 第十二章 : 动态内存之shared_ptr与new的结合使用、智能指针异常

    shared_ptr和new结合使用 一个shared_ptr默认初始化为一个空指针.我们也可以使用new返回的指针来初始化一个shared_ptr: shared_ptr<double> ...

  8. tulterbot遥感操作使用Interactive Markers--12

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.安装ros indigo功能包: sudo apt-get install ros-indigo-t ...

  9. 不小心误删@‘local’操作恢复

    今天在测试用户权限的时候不小心把User: ''@'localhost';用户删除了 导致任何用户登录都无权限操作 恢复过程 停止mysql服务:在mysql安装目录下找到my.ini(linux下是 ...

  10. MySQL-python模块

    1.  Python 操作 Mysql 模块的安装 linux: pip install MySQL-python 或 yum -y install MySQL-python windows: exe ...