Giter Site home page Giter Site logo

vessel's Introduction

Vessel - high-level web crawling framework

Fast as Chrome, dead simple and yet extendable.

It is Ruby high-level web crawling framework based on Ferrum for extracting the data you need from websites. It can be used in a wide range of scenarios, like data mining, monitoring or historical archival. For automated testing we recommend Cuprite.

Install

Add this to your Gemfile:

gem "vessel"

A look around

In order to show you how Vessel works we are going to crawl together famous quotes website:

require "json"
require "vessel"

class QuotesToScrapeCom < Vessel::Cargo
  domain "quotes.toscrape.com"
  start_urls "https://quotes.toscrape.com/tag/humor/"

  def parse
    css("div.quote").each do |quote|
      yield({
        author: quote.at_xpath("span/small").text,
        text: quote.at_css("span.text").text
      })
    end

    if next_page = at_xpath("//li[@class='next']/a[@href]")
      url = absolute_url(next_page.attribute(:href))
      yield request(url: url, handler: :parse)
    end
  end
end

quotes = []
QuotesToScrapeCom.run { |q| quotes << q }
puts JSON.generate(quotes)

Save this to quotes.rb file and run bundle exec ruby quotes.rb > quotes.json. When this finishes you will have a list of the quotes in JSON format in the quotes.json file.

How it all works? First Vessel using Ferrum spawns Chrome which goes to one or more urls in start_urls, in our case it's only one. After Chrome reports back that page is loaded with all the resources it needs the first default handler parse is invoked. In the parse handler, we loop through the quote elements using a CSS Selector, yield a Hash with the extracted quote text and author and look for a link to the next page and schedule another request using the same parse method as a handler.

Notice that all requests are scheduled and handled concurrently. We use thread pool to work with all your requests with one page per core by default or add threads max: n to a class. If you yield more than one request Ruby will send them to Chrome which will load pages in parallel. Thus crawler is lightweight and speedy.

Settings

  • domain
  • start_urls
  • driver
  • delay
  • headers
  • cookies
  • threads
  • middleware
  • proxy
  • blacklist
  • whitelist

Headers

class MyScraper < Vessel::Cargo
  headers "Content-Type" => "text/plain",
          "Referer" => "http://example.com"
end

Headful mode

You can disable headless mode by passing driver_options settings:

MyScraper.run(driver_options: { headless: false })

Selectors

  • at_css
  • css
  • at_xpath
  • xpath

Middleware

To be continued

License

The gem is available as open source under the terms of the MIT License.

vessel's People

Contributors

glaucocustodio avatar mifrill avatar milk1000cc avatar pandawhisperer avatar petergoldstein avatar pweldon avatar route avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.