Skip to content

hpneo/scrap_kit

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ScrapKit

ScrapKit automates web scraping and converts the results in plain objects by using configuration objects called recipes.

Each recipe can be loaded as an object or as JSON file, and have the following structure:

{
  "url": "https://status.heroku.com/",
  "attributes": {
    "apps": ".subnav__inner .ember-view:nth-child(1) > .status-summary__description",
    "data": ".subnav__inner .ember-view:nth-child(2) > .status-summary__description",
    "tools": ".subnav__inner .ember-view:nth-child(3) > .status-summary__description"
  }
}
  • url: It defines the web page to scrape.
  • attributes: Is an object that maps each attribute name with its corresponding CSS selector.

attributes can have a more complex structure to handle collections. For example:

{
  "url": "https://hpneo.dev/",
  "attributes": {
    "posts": {
      "selector": ".post-item",
      "children_attributes": {
        "title": "h2"
      }
    }
  }
}

In this case attributes has a posts key, which will store the results of a collection, defined by a CSS selector and an object of children attributes.

children_attributes is an object that maps each attribute name with its corresponding CSS selector (similar to how attributes works in its simpler version).

Installation

Add this line to your application's Gemfile:

gem 'scrap_kit'

And then execute:

$ bundle

Or install it yourself as:

$ gem install scrap_kit

Usage

ScrapKit::Recipe.load can take an object with the recipe, or load a JSON file.

recipe = ScrapKit::Recipe.load(
  url: "https://status.heroku.com/",
  attributes: {
    apps: ".subnav__inner .status-summary:nth-child(1) > .status-summary__description",
    data: ".subnav__inner .status-summary:nth-child(2) > .status-summary__description",
    tools: ".subnav__inner .status-summary:nth-child(3) > .status-summary__description",
  }
)

output = recipe.run
#=> {:apps=>"ok", :data=>"ok", :tools=>"ok"}

For more complex structures it's recommended to store the recipe in a JSON file:

recipe = ScrapKit::Recipe.load("./spec/fixtures/file.json")

output = recipe.run
#=> {:posts=>[{:title=>"APIs de Internacionalización en JavaScript"}, {:title=>"Ejecutando comandos desde Ruby"}, {:title=>"Usando Higher-Order Components"}]}

Working with selectors

Each attribute can be mapped to a selector, which can be any of the following types:

  • A string, which represents a CSS selector.
".subnav__inner .ember-view:nth-child(1) > .status-summary__description"
  • A hash, which can have any of the following options:
    • xpath: [String]
    • css: [String]
    • index: [Integer]
    • tag_name: [String]
    • text: [String]
{ text: "View Archive" }
  • An array, which represents a path of selectors, where its last item must be a hash that matches a selector with an expected value.
[".up-time-chart", { ".region-header .u-margin-Tm": "REGION" }]

Use any of them as it suits you best.

Writing steps

Recipes can have a steps entry. This entry defines previous actions the scraper have to follow before extract the attributes. The following steps are supported:

  • goto: It instructs the scraper to go to a link inside the current page. Its value can be a hash or array selector, or a URL:
{
  goto: { text: "View Archive" }
}
  • click: It instructs the scraper to click on an element inside the current page. Its value can be a hash or array selector:
{
  click: { css: "[type=submit]" }
}
  • fill_form: It instructs the scraper to fill a form or any form field inside the current page. Its value is a hash where the keys are either a input's name or a CSS selector, and the values are the values to be entered into those fields:
{
  fill_form: {
    gem_name: "ScrapKit",
    author: "hpneo",
  }
}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hpneo/scrap_kit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the ScrapKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Scrap web sites using JSON-based recipes.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published