GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - microfisher/Simple-Web-Crawler: 基于C#.NET的简单网页爬虫,支持异步并发、切换代理、操作Cookie、Gzip加速。

BranchesTags
Open more actions menu

Latest commit

 

History

20 Commits
20 Commits

Folders and files

NameName
Last commit message
Last commit date
Wesley.Crawler.SimpleCrawler
Wesley.Crawler.SimpleCrawler
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
SimpleCrawler.sln
SimpleCrawler.sln
 
 

Repository files navigation

简单且高效的网站爬虫

基于C#.NET的简单网页爬虫,支持异步并发、设置代理、操作Cookie、Gzip页面加速。

主要特性

  • 支持Gzip根据网页内容自动解压,加快爬虫载入速度;
  • 支持异步并发抓取;
  • 支持自动事件通知;
  • 支持代理切换;
  • 支持操作Cookies;

运行截图

  • 抓取城市列表

使用正则表达式清洗数据

  • 抓取酒店列表

抓取城市下的酒店列表

示例代码

    /// <summary>
    /// 抓取城市列表
    /// </summary>
    public static void CityCrawler() {
        
        var cityUrl = "http://hotels.ctrip.com/citylist";//定义爬虫入口URL
        var cityList = new List<City>();//定义泛型列表存放城市名称及对应的酒店URL
        var cityCrawler = new SimpleCrawler();//调用刚才写的爬虫程序
        cityCrawler.OnStart += (s, e) =>
        {
            Console.WriteLine("爬虫开始抓取地址:" + e.Uri.ToString());
        };
        cityCrawler.OnError += (s, e) =>
        {
            Console.WriteLine("爬虫抓取出现错误:" + e.Uri.ToString() + ",异常消息:" + e.Exception.Message);
        };
        cityCrawler.OnCompleted += (s, e) =>
        {
            //使用正则表达式清洗网页源代码中的数据
            var links = Regex.Matches(e.PageSource, @"<a[^>]+href=""*(?<href>/hotel/[^>\s]+)""\s*[^>]*>(?<text>(?!.*img).*?)</a>", RegexOptions.IgnoreCase);
            foreach (Match match in links)
            {
                var city = new City
                {
                    CityName = match.Groups["text"].Value,
                    Uri = new Uri("http://hotels.ctrip.com" + match.Groups["href"].Value
                )
                };
                if (!cityList.Contains(city)) cityList.Add(city);//将数据加入到泛型列表
                Console.WriteLine(city.CityName + "|" + city.Uri);//将城市名称及URL显示到控制台
            }
            Console.WriteLine("===============================================");
            Console.WriteLine("爬虫抓取任务完成!合计 " + links.Count + " 个城市。");
            Console.WriteLine("耗时:" + e.Milliseconds + "毫秒");
            Console.WriteLine("线程:" + e.ThreadId);
            Console.WriteLine("地址:" + e.Uri.ToString());
        };
        cityCrawler.Start(new Uri(cityUrl)).Wait();//没被封锁就别使用代理:60.221.50.118:8090
    }

About

基于C#.NET的简单网页爬虫,支持异步并发、切换代理、操作Cookie、Gzip加速。

Resources

Readme
Activity

Stars

288 stars

Watchers

27 watching

Forks

158 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.