- report – output a csv with data from the crawl
- export – creates a local replication of the data on the server based off the url structure. Text data is stored in yaml format.
: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
– urls from a tags within the resource:images
– urls from img tags within the resource:related
– urls from link tags:scripts
– urls from script tags:styles
– urls from within link tags with rel of stylesheet and from url() directives with stylesheets
:average_length
– average size of each objet:minimum_length
– minimum length returned:queued_at
– date and time that the crawl was started at (eg: “2012-09-10T23:10:08+01:00”):maximum_length
– maximum length of object received:status_counts
– hash with the status returned as the key and value as number of pages (eg: {"404" => 1, “200” => 1}):mime_counts
– hash containing the mime type as key and count or pages as value (eg: {"text/html" => 8, “image/jpeg” => 25)}):queue_counter
– size of queue waiting to be processed for crawl:page_count
– number of html pages retrieved:total_length
– total size of data received:current_status
– Current status of crawl:asset_count
– count of non-html objects received:page_size
– total size of pages received:average_response_time
– average response time of all objects:crawl_counter
– number of objects that have been crawled:minimum_response_time
– quickest response time of crawl:maximum_response_time
– longest response time of crawl:asset_size
– total size of all non-assets received
Install crawler as a gem
gem install cobweb
or in a Gemfile
gem 'cobweb'
Creates a new crawler object based on a base_url
- options – The following hash keys can be defined:
: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: ‘CobwebProcessJob’ when using resque, ‘CrawlProcessWorker’ when using sidekiq):crawl_finished_queue
– specifies the processing queue for statistics to be sent to after finishing crawling (Default: ‘CobwebFinishedJob’ when using resque, ‘CrawlFinishedWorker’ when using sidekiq):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):seed_urls
– an array of urls that are put into the queue regardless of any other setting, combine with {:external_urls => "*"} to limit to seed 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['*/*']
:direct_call_process_job
– boolean that specifies whether objects should be passed directly to a processing method or should be put onto a queue:raise_exceptions
– defaults to handling exceptions with debug output, setting this to true will raise exceptions in your app:use_encoding_safe_process_job
– Base64-encode the body when storing job in queue; set to true when you are expecting non-ASCII content (Default: false):proxy_addr
– hostname of a proxy to use for crawling (e. g., ‘myproxy.example.net’, default: nil):proxy_port
– port number of the proxy (default: nil):treat_https_as_http
– determines whether https and http urls are treated as the same (defaults to true, ie treated as the same)
crawler = Cobweb.new(:follow_redirects => false)
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/")
Simple get that obey’s the options supplied in new.
url
– url requested
crawler.get("http://www.google.com/")
Simple get that obey’s the options supplied in new.
url
– url requested
crawler.head("http://www.google.com/")
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.
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.
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.
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
thread_count
– specifies the number of threads used by the crawler, defaults to 1
The CobwebCrawlHelper class is a helper class to assist in getting information about a crawl and to perform functions against the crawl
crawl = CobwebCrawlHelper.new(options)
options
– the hash of options passed into Cobweb.new (must include a:crawl_id
)
- 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
- Refactor into a module and refactor class names to remove cobweb and increase simplicity
Copyright © 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.