Class: SpiderBot::Crawl

Inherits:
Object
  • Object
show all
Defined in:
lib/spider_bot/crawl.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Crawl

Initialize a new Spider Bot

Parameters:

  • url (String)

    the spider target website curl

  • options (Hash) (defaults to: {})

    the spider crawl configurate options

Options Hash (options):

  • :type (Symbol)

    the request body format, :html or :json

  • :headers (Hash)

    the custom request headers

  • :path, (String)

    the custom request path

  • :query (Hash)

    the request query

  • :user_agent (String)

    the custom request user agent

  • :source (Boolean)
  • :data (Proc)

    get crawl data list in body

  • :first (Proc)

    get crawl data list first item

  • :last (Porc)

    get crawl data list last item

  • :encode (String)

    custom request encode



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spider_bot/crawl.rb', line 19

def initialize(url, options = {})
  parse_uri = URI.parse url
  @uri = parse_uri.scheme + "://" + parse_uri.host
  
  # don't add 443 port append to url when access https website
  if !["80", "443"].include?(parse_uri.port.to_s)
    @uri = @uri + ":" + parse_uri.port.to_s 
  end
  
  @origin_path = parse_uri.path || "/"
  
  @origin_type = options[:type] || :html
  @origin_headers = options[:headers] || {}
  @origin_query = options[:query] || {}

  @origin_user_agent = options[:user_agent] || "Mac Safari"
  @origin_source = options[:source] || false

  @origin_data = options[:data]
  @origin_first = options[:first]
  @origin_last = options[:last]

  @origin_encode = options[:encode]
  
  @page_path = @origin_path
  @page_type = @origin_type
  @page_headers = @origin_headers || {}
  @page_query = {}

  @page_data = @origin_data
  @page_first = @origin_first
  @page_last = @origin_last

  @page_start = 1
  @page_add = 1
  @page_expire = 10
  @page_sleep = 0
  
  @paginate_last = nil
  @paginate_error = 0
  @paginate_type = :html
  @paginate_path = ""
  @paginate_query = {}
  
  @connection = Http::Client.new do |http| 
    http.url= @uri
    http.user_agent= @origin_user_agent
    http.headers= @origin_headers
  end
end

Instance Method Details

#crawl_data(&block) ⇒ Object

Process crawl data

Parameters:

  • a (block)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spider_bot/crawl.rb', line 74

def crawl_data(&block)
  @paginate_num = @page_start
  
  catch :all do
    begin
      crawl_response = crawl_request(@origin_path, @origin_query, @origin_type, @origin_data, @origin_first, @origin_last, &block)
      return crawl_response if !block_given?
      process_response(crawl_response, &block)
    rescue Exception => e
      handle_error(e)
      crawl_data(&block)
    end
    
    @paginate_error = 0
    return if @page_query.blank? && @page_path == @origin_path
    
    crawl_paginate(&block) 
  end
end