stewartmckee / cobweb

Web crawler with very flexible crawling options. Can either use standalone or can be used with resque to perform clustered crawls.
MIT License
227 stars 45 forks source link

h1. Cobweb v1.1.0

"@cobweb_gem":https://twitter.com/cobweb_gem !https://badge.fury.io/rb/cobweb.png!:http://badge.fury.io/rb/cobweb !https://circleci.com/gh/stewartmckee/cobweb.svg?style=shield&circle-token=07357f0bd17ac67e21ea161fb9abdb35ecac4c2e! !https://gemnasium.com/stewartmckee/cobweb.png! !https://coveralls.io/repos/stewartmckee/cobweb/badge.png?branch=master(Coverage Status)!:https://coveralls.io/r/stewartmckee/cobweb

h2. Intro

CobWeb has three methods of running. Firstly it is a http client that allows get and head requests returning a hash of data relating to the requested resource. The second main function is to utilize this combined with the power of Resque to cluster the crawls allowing you crawl quickly. Lastly you can run the crawler with a block that uses each of the pages found in the crawl.

I've created a sample app to help with setting up cobweb at http://github.com/stewartmckee/cobweb_sample

h3. Resque

When running on resque, passing in a Class and queue name it will enqueue all resources to this queue for processing, passing in the hash it has generated. You then implement the perform method to process the resource for your own application.

h3. Standalone

CobwebCrawler takes the same options as cobweb itself, so you can use any of the options available for that. An example is listed below.

While the crawler is running, you can view statistics on http://localhost:4567

h3. Command Line

Cobweb can also be ran from the command line to perform various pre-canned tasks.

h3. Data Returned For Each Page The data available in the returned hash are:

h3. Statistics

Statistics are available during the crawl, you can create a Stats object passing in a hash with redis_options and crawl_id. Stats has a get_statistics method that returns a hash of the statistics available to you. It is also returned by default from the CobwebCrawler.crawl standalone crawling method.

The data available within statistics is as follows:

h2. Installation

Install crawler as a gem

bc. gem install cobweb

or in a @Gemfile@

bc. gem 'cobweb'

h2. Usage

h3. Cobweb

h4. new(options)

Creates a new crawler object based on a base_url

bc. crawler = Cobweb.new(:follow_redirects => false)

h4. start(base_url)

Starts a crawl through resque. Requires the @:processing_queue@ to be set to a valid class for the resque job to work with the data retrieved.

Once the crawler starts, if the first page is redirected (eg from http://www.test.com to http://test.com) then the endpoint scheme, host and domain is added to the internal_urls automatically.

bc. crawler.start("http://www.google.com/")

h4. get(url)

Simple get that obey's the options supplied in new.

bc. crawler.get("http://www.google.com/")

h4. head(url)

Simple get that obey's the options supplied in new.

bc. crawler.head("http://www.google.com/")

h4. Processing Queue

The @:processing_queue@ option is used to specify the class that contains the resque perform method to pass the content onto. This class should be defined in your application to perform any tasks you wish to the content. There are two options however, for running this. Firstly, the default settings will push the content crawled onto a resque queue for that class. This allows you the flexibility of running in queues on seperate machines etc. The main drawback to this is that all your content is stored in redis within the queue. This can be memory intensive if you are crawling large sites, or have large content that is being crawled. To get around this you can specify that the crawl_job calls the perform method on the processing queue class directly, thereby not using memory in redis for the content. This is performed by using the :direct_call_process_job. If you set that option to 'true' then instead of the job being queued, it will be executed within the crawl_job queue.

h3. CobwebCrawler

CobwebCrawler is the standalone crawling class. If you don't want to use resque or sidekiq and just want to crawl the site within your ruby process, you can use this class.

bc. crawler = CobwebCrawler.new(:cache => 600) statistics = crawler.crawl("http://www.pepsico.com")

You can also run within a block and get access to each page as it is being crawled.

bc. statistics = CobwebCrawler.new(:cache => 600).crawl("http://www.pepsico.com") do |page| puts "Just crawled #{page[:url]} and got a status of #{page[:status_code]}." end puts "Finished Crawl with #{statistics[:page_count]} pages and #{statistics[:asset_count]} assets."

There are some specific options for CobwebCrawler in addition to the normal cobweb options

h3. CobwebCrawlHelper

The CobwebCrawlHelper class is a helper class to assist in getting information about a crawl and to perform functions against the crawl

bc. crawl = CobwebCrawlHelper.new(options)

h2. Contributing/Testing

Firstly, you could Buy Me A Coffee

Feel free to contribute small or large bits of code, just please make sure that there are rspec test for the features your submitting. We also test on travis at http://travis-ci.org/#!/stewartmckee/cobweb if you want to see the state of the project.

Continuous integration testing is performed by the excellent Travis: http://travis-ci.org/#!/stewartmckee/cobweb

h2. Todo

h3. Big changes

h2. License

h3. The MIT License

Copyright (c) 2013 Active Information Design

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.