spidr

postmodernMIT更新放缓

Ruby 编写的多功能网页爬虫库,可抓取单个站点、多个域名或指定链接,也能无限爬取,主打快速易用。

A versatile Ruby web spidering library that can spider a site, multiple domains, certain links or infinitely. Spidr is designed to be fast and easy to use.

v0.7.2发布于 2025-02-03 07:59 UTC836star104fork同步于 2026-11-07 07:34 UTC

怎么上手

由 README 提炼,执行前请确认
gem install spidr
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Ruby 网页爬虫库,可爬单站、多域名或无限链接,快速易用

Ruby 编写的多功能网页爬虫库,可抓取单个站点、多个域名或指定链接,也能无限爬取,主打快速易用。

核心能力

  • 跟踪 a、iframe、frame 标签及重定向链接
  • 支持按 scheme、主机、端口、扩展名黑白名单过滤
  • 提供页面、URL、链接、失败请求等回调钩子
  • 支持 robots.txt、Basic Auth、Cookie 与代理
  • 可从上次会话恢复爬取队列与历史

容易踩的坑

需 Ruby >= 2.0.0 且依赖 nokogiri ~> 1.3;README 未说明其他坑

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

1 个版本v0.7.2
  1. v0.7.20.7.2最新正式版2025-02-03 · 2025-02-03 07:59 UTC
    • Added the base64 gem as a dependency to satisfy Bundler.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Spidr

    CI

    Description

    Spidr is a versatile Ruby web spidering library that can spider a site, multiple domains, certain links or infinitely. Spidr is designed to be fast and easy to use.

    Features

    • Follows:
      • a tags.
      • iframe tags.
      • frame tags.
      • Cookie protected links.
      • HTTP 300, 301, 302, 303 and 307 Redirects.
      • Meta-Refresh Redirects.
      • HTTP Basic Auth protected links.
    • Black-list or white-list URLs based upon:
      • URL scheme.
      • Host name
      • Port number
      • Full link
      • URL extension
      • Optional /robots.txt support.
    • Provides callbacks for:
      • Every visited Page.
      • Every visited URL.
      • Every visited URL that matches a specified pattern.
      • Every origin and destination URI of a link.
      • Every URL that failed to be visited.
    • Provides action methods to:
      • Pause spidering.
      • Skip processing of pages.
      • Skip processing of links.
    • Restore the spidering queue and history from a previous session.
    • Custom User-Agent strings.
    • Custom proxy settings.
    • HTTPS support.

    Examples

    Start spidering from a URL:

    Spidr.start_at('https://www.ruby-lang.org/en/') do |agent|
      # ...
    end
    

    Spider a host:

    Spidr.host('www.ruby-lang.org') do |agent|
      # ...
    end
    

    Spider a domain (and any sub-domains):

    Spidr.domain('ruby-lang.org') do |agent|
      # ...
    end
    

    Spider a site:

    Spidr.site('https://www.ruby-lang.org/') do |agent|
      # ...
    end
    

    Spider multiple hosts:

    Spidr.start_at('https://www.ruby-lang.org/en/', hosts: ['ruby-lang.org', /.*\.ruby-lang\.org\z/]) do |agent|
      # ...
    end
    

    Do not spider certain links:

    Spidr.site('https://www.ruby-lang.org/', ignore_links: [%r{\A/blog/}]) do |agent|
      # ...
    end
    

    Do not spider links on certain ports:

    Spidr.site('https://www.ruby-lang.org/', ignore_ports: [8000, 8010, 8080]) do |agent|
      # ...
    end
    

    Do not spider links blacklisted in robots.txt:

    Spidr.site('https://www.ruby-lang.org/', robots: true) do |agent|
      # ...
    end
    

    Print out visited URLs:

    Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_url { |url| puts url }
    end
    

    Build a URL map of a site:

    url_map = Hash.new { |hash,key| hash[key] = [] }
    
    Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_link do |origin,dest|
        url_map[dest] << origin
      end
    end
    

    Print out the URLs that could not be requested:

    Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_failed_url { |url| puts url }
    end
    

    Finds all pages which have broken links:

    url_map = Hash.new { |hash,key| hash[key] = [] }
    
    spider = Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_link do |origin,dest|
        url_map[dest] << origin
      end
    end
    
    spider.failures.each do |url|
      puts "Broken link #{url} found in:"
    
      url_map[url].each { |page| puts "  #{page}" }
    end
    

    Search HTML and XML pages:

    Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_page do |page|
        puts ">>> #{page.url}"
    
        page.search('//meta').each do |meta|
          name = (meta.attributes['name'] || meta.attributes['http-equiv'])
          value = meta.attributes['content']
    
          puts "  #{name} = #{value}"
        end
      end
    end
    

    Print out the titles from every page:

    Spidr.site('https://www.ruby-lang.org/') do |spider|
      spider.every_html_page do |page|
        puts page.title
      end
    end
    

    Print out every HTTP redirect:

    Spidr.host('www.ruby-lang.org') do |spider|
      spider.every_redirect_page do |page|
        puts "#{page.url} -> #{page.headers['Location']}"
      end
    end
    

    Find what kinds of web servers a host is using, by accessing the headers:

    servers = Set[]
    
    Spidr.host('www.ruby-lang.org') do |spider|
      spider.all_headers do |headers|
        servers << headers['server']
      end
    end
    

    Pause the spider on a forbidden page:

    Spidr.host('www.ruby-lang.org') do |spider|
      spider.every_forbidden_page do |page|
        spider.pause!
      end
    end
    

    Skip the processing of a page:

    Spidr.host('www.ruby-lang.org') do |spider|
      spider.every_missing_page do |page|
        spider.skip_page!
      end
    end
    

    Skip the processing of links:

    Spidr.host('www.ruby-lang.org') do |spider|
      spider.every_url do |url|
        if url.path.split('/').find { |dir| dir.to_i > 1000 }
          spider.skip_link!
        end
      end
    end
    

    Requirements

    Install

    $ gem install spidr
    

    License

    See {file:LICENSE.txt} for license information.