提取網頁中的超連結(C#)

來源:互聯網
上載者:User

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 = @"\.|/$";
  retVal = Regex.Replace(retVal, strRegex, "").ToString();

  if(retVal == "")
   retVal = "other";

  return retVal;
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.