简单的C#爬虫

本文介绍如何利用C#中的WebClient类抓取网页内容,并通过正则表达式解析特定信息,包括电子邮件地址和超链接等。示例代码展示了从拉勾网首页抓取招聘信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Net;  
using System.Text;  
using System.Text.RegularExpressions;  
using System.Threading.Tasks;  
  
namespace _2015._5._23通过WebClient类发起请求并下载html  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            #region 抓取网页邮箱  
            //string url = "http://zhidao.baidu.com/link?url=cvF0de2o9gkmk3zW2jY23TLEUs6wX-79E1DQVZG7qaBhEVT_xlh6TO7p0W4qwuAZ_InLymC_-mJBBcpdbzTeq_";  
            //WebClient wc = new WebClient();  
            //wc.Encoding = Encoding.UTF8;  
            //string str = wc.DownloadString(url);  
            //MatchCollection matchs=  Regex.Matches(str,@"\w+@([-\w])+([\.\w])+",RegexOptions.ECMAScript);  
            //foreach (Match item in matchs)  
            //{  
            //    Console.WriteLine(item.Value);  
            //}  
            //Console.WriteLine(matchs.Count);  
            #endregion   
 
            #region 抓取网页图片  
  
            //WebClient wc = new WebClient();  
            //wc.Encoding = Encoding.UTF8;  
            ////下载源网页代码  
            //string html = wc.DownloadString("http://dongxi.douban.com/?dcs=top-nav&dcm=douban");  
            //MatchCollection matches= Regex.Matches(html,"<img.*src=\"(.+?)\".*>");  
            //foreach (Match item in matches)  
            //{  
            //    //下载图片到指定路径  
            //    wc.DownloadFile(item.Groups[1].Value,@"c:\mv\"+Path.GetFileName(item.Groups[1].Value));  
            //}  
            //Console.WriteLine(matches.Count);  
 
            #endregion 爬一些信息  
  
            WebClient wc = new WebClient();  
            wc.Encoding = Encoding.UTF8;  
            string html = wc.DownloadString("http://www.lagou.com/");  
  
            MatchCollection matches= Regex.Matches(html,"<a.*jobs.*>(.*)</a>");  
            foreach (Match item in matches)  
            {  
                Console.WriteLine(item.Groups[1].Value);  
            }  
            Console.WriteLine(matches.Count);  
            Console.ReadKey();                                    
        }  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值