转载: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. 作业3-个人项目<词频统计>

    上了一天的课,现在终于可以静下来更新我的博客了.       越来越发现,写博客是一种享受.来看看这次小林老师的“作战任务”.                词频统计 单词: 包含有4个或4个以上的字 ...

  2. 关于linux python vim的一些基础知识(很零散)

    清空文件夹filenmae下所有文件 rm filename/* vim复制大量代码段 num+yy 从光标起始处复制num个数行 然后 python: 设置中断 1.from IPython imp ...

  3. wcf之OperationContextScope

    作用:使用消息头向服务发送额外的信息. 1.客户端代码如下: namespace Client { class Program { static void Main(string[] args) { ...

  4. ButterKnife View 注入

    /***************************************************************************************** * ButterK ...

  5. apache配置文件 httpd-vhosts.conf 和 htaccess

    1.apache多站点配置中ServerAlias什么意思? -:就是给ServerName起别名,通过Alias中的域名也可以访问这个虚拟主机. eg: <VirtualHost www.be ...

  6. setTimeout方法

    //不建议传递字符串 setTimeout(alert("hello"),1000); //推荐调用方式 setTimeout(function(){alert("hel ...

  7. 用lucene4.10.2分词器进行分词

    import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.luc ...

  8. CentOS6编译装载nbd模块

    今天突然发现CentOS系统没有nbd模块,只能重新装下,下面记录下整个编译过程: 系统:CentOS6.5 内核:2.6.32-431.el6.x86_64 [root@localhost ~]# ...

  9. andorid中Html.fromHtml方法

    在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本框中的字符串进行HTML格式化,支持的还是很多的, 但要注意的是要在string.xml中用<!--cda ...

  10. nginx服务配置---php服务接入

    前言: 最近要搭建一个内部的wiki系统, 网上搜了一圈, 也从知乎上搜集了一些大神的评价和推荐. 重点找了几个开源的wiki系统, 不过发现他们都是采用php来实现的. 于是乎需要配置php环境, ...