Skip to content

Web crawler with very flexible crawling options. Can either use standalone or can be used with resque to perform clustered crawls.

Notifications You must be signed in to change notification settings

thomasdavis/cobweb

 
 

Repository files navigation

Cobweb v0.0.62

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.

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.

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.

crawler = CobwebCrawler.new(:cache => 600);

stats = crawler.crawl(“http://www.pepsico.com”)

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

Data Returned
The data available in the returned hash are:

  • :url – url of the resource requested
  • :status_code – status code of the resource requested
  • :mime_type – content type of the resource
  • :character_set – character set of content determined from content type
  • :length – length of the content returned
  • :body – content of the resource
  • :location – location header if returned
  • :redirect_through – if your following redirects, any redirects are stored here detailing where you were redirected through to get to the final location
  • :headers – hash or the headers returned
  • :links – hash or links on the page split in to types
    • :links – url’s from a tags within the resource
    • :images – url’s from img tags within the resource
    • :related – url’s from link tags
    • :scripts – url’s from script tags
    • :styles – url’s from within link tags with rel of stylesheet and from url() directives with stylesheets
The source for the links can be overridden, contact me for the syntax (don’t have time to put it into this documentation, will as soon as i have time!)

Installation

Install crawler as a gem

gem install cobweb

Usage

new(options)

Creates a new crawler object based on a base_url

  • options – Options are passed in as a hash,
  • :follow_redirects – transparently follows redirects and populates the :redirect_through key in the content hash (Default: true)
  • :redirect_limit – sets the limit to be used for concurrent redirects (Default: 10)
  • :processing_queue – specifies the processing queue for content to be sent to (Default: ContentProcessJob)
  • :debug – enables debug output (Default: false)
  • :quiet – hides default output (Default: false)
  • :cache – sets the ttl for caching pages, set to nil to disable caching (Default: 300)
  • :timeout – http timeout for requests (Default: 10)
  • :redis_options – hash containing the initialization options for redis (e.g. {:host => “redis.mydomain.com”}) (Default: {})
  • :internal_urls – array of strings representing internal url forms for your site (eg: [‘http://test.com/*’, ‘http://blog.test.com/*’, ‘http://externaltest.com/*’]) (Default: [], although your first url’s scheme, host and domain are added)
  • :first_page_redirect_internal – if true and the first page crawled is a redirect, it will add the final destination of redirects to the internal_urls (e.g. http://www.test.com gets redirected to http://test.com) (Default: true)
  • :crawl_id – the id used internally for identifying the crawl. Can be used by the processing job to seperate crawls
  • :internal_urls – an array of urls with * wildcards that represent urls internal to the site (ie pages within the same domain)
  • :external_urls – an array of urls with * wildcards that represent urls external to the site (overrides internal_urls)
  • :obey_robots – boolean determining if robots.txt should be honoured. (default: false)
  • :user_agent – user agent string to match in robots.txt (not sent as user_agent of requests yet) (default: cobweb)
  • :crawl_limit_by_page – sets the crawl counter to only use html page types when counting objects crawled
  • :valid_mime_types – an array of mime types that takes wildcards (eg ‘text/*’) defaults to [‘/’]

crawler = CobWeb.new(:follow_redirects => false)

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.

  • base_url – the url to start the crawl from

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.

crawler.start(“http://www.google.com/”)

get(url)

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

  • url – url requested

crawler.get(“http://www.google.com/”)

head(url)

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

  • url – url requested

crawler.head(“http://www.google.com/”)

Contributing/Testing

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

Todo

  • Tidy up classes with link parsing
  • Refactoring of code to simplify design
  • Remove requirement of redis from standalone crawler
  • Add redis settings to standalone crawler (ie to connect to remote redis)
  • Add ability to start and stop crawls from web interface
  • Allow crawler to start as web interface only (ie not run crawls at start)
  • Fix content encoding issue requiring separate process job
  • DRY the cobweb get/head calls, its got a lot of duplication
  • Investigate using event machine for single threaded crawling

Big changes

  • Refactor into a module and refactor class names to remove cobweb and increase simplicity

License

The MIT License

Copyright © 2010 6Central Limited

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.

About

Web crawler with very flexible crawling options. Can either use standalone or can be used with resque to perform clustered crawls.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 91.5%
  • Ruby 8.5%