C#程序抓取网页实例

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Net;  
  9. using System.IO;  
  10. using System.Text.RegularExpressions;  
  11. using System.Collections;  
  12. namespace CopyHtml  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             //获取指定网页中的源数据  
  23.             string rl;  
  24.             WebRequest Request = WebRequest.Create(textBox1.Text.Trim());  
  25.             WebResponse Response = Request.GetResponse();  
  26.             Stream resStream = Response.GetResponseStream();  
  27.             StreamReader sr = new StreamReader(resStream, Encoding.Default);  
  28.             StringBuilder sb = new StringBuilder();  
  29.             while ((rl = sr.ReadLine()) != null)  
  30.             {  
  31.                 sb.Append(rl);  
  32.             }  
  33.             textBox2.Text = sb.ToString();//抓取得到的源网页  
  34.             string he = textBox2.Text.ToString();  
  35.             textBox3.Text = stripHtml(he);//去除html标签后得到的源网页  
  36.             Match TitleMatch = Regex.Match(he, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);//获取网页的标题  
  37.             string title = TitleMatch.Groups[1].Value;  
  38.             textBox4.Text = ("网页的标题是:" + title );  
  39.         }  
  40.         /// <summary>  
  41.         /// 去掉网页中的html标签  
  42.         /// </summary>  
  43.         /// <param name="strHtml">待转化的字符串</param>  
  44.         /// <returns></returns>  
  45.         private string stripHtml(string strHtml)  
  46.         {  
  47.             Regex objRegExp = new Regex("<(.|/n)+?>");  
  48.             string strOutput = objRegExp.Replace(strHtml, "");  
  49.             strOutput = strOutput.Replace("<""<");  
  50.             strOutput = strOutput.Replace(">"">");  
  51.             return strOutput;  
  52.         }  
  53.         // 提取HTML代码中的网址   
  54.         public static ArrayList GetHyperLinks(string htmlCode)  
  55.         {  
  56.             ArrayList al = new ArrayList();  
  57.             string strRegex = @"(href)[ ]*=[ ]*[""'][^""'#>]+[""']";  
  58.             Regex r = new Regex(strRegex, RegexOptions.IgnoreCase);  
  59.             MatchCollection m = r.Matches(htmlCode);  
  60.             for (int i = 0; i <= m.Count - 1; i++)  
  61.             {  
  62.                 bool rep = false;  
  63.                 string strNew = m[i].ToString();  
  64.                 // 过滤重复的URL   
  65.                 foreach (string str in al)  
  66.                 {  
  67.                     if (strNew == str)  
  68.                     {  
  69.                         rep = true;  
  70.                         break;  
  71.                     }  
  72.                 }  
  73.                 if (!rep) al.Add(strNew);  
  74.             }  
  75.             al.Sort();  
  76.             return al;  
  77.         }  
  78.     }  
  79. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值