Class: HTTP::Response

Inherits:
Object show all
Defined in:
lib/http_crawler/http/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#validationsObject

验证码判断



53
54
55
# File 'lib/http_crawler/http/response.rb', line 53

def validations
  @validations
end

Instance Method Details

#decoding_bodyObject

解压并转码 body 数据



5
6
7
# File 'lib/http_crawler/http/response.rb', line 5

def decoding_body
  @decoding_body ||= self.body.to_s
end

#get_date(str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/http_crawler/http/response.rb', line 36

def get_date(str)
  time = Time.now
  case str
  when /^(\d{1,2})小时前$/
    time = time - $1.to_i.hours
  when /^(\d{1,2})月(\d{1,2})日$/
    time = Time.local(time.year, $1.to_i, $2.to_i)
  when /^(\d{4})年(\d{1,2})月(\d{1,2})日$/
    time = Time.local($1.to_i, $2.to_i, $3.to_i)
  when /^(\d{1,2})月(\d{1,2})日[ ]{0,3}(\d{1,2}):(\d{1,2})$/ # 09月30日 12:04
    time = Time.local(time.year, $1.to_i, $2.to_i, $3.to_i, $4.to_i)
  end
  return time
end

#htmlObject

def decoding_body



11
12
13
# File 'lib/http_crawler/http/response.rb', line 11

def html
  @html ||= Nokogiri::HTML(decoding_body)
end

#jsonObject



15
16
17
18
19
# File 'lib/http_crawler/http/response.rb', line 15

def json
  @json ||= JSON.parse(decoding_body)
  @json = JSON.parse(@json) if String === @json
  @json
end

#parsingObject

解析



27
28
29
# File 'lib/http_crawler/http/response.rb', line 27

def parsing
  json
end

#readabilityObject

通过readability 解析数据



22
23
24
# File 'lib/http_crawler/http/response.rb', line 22

def readability
  @readability ||= Readability::Document.new(decoding_body, {do_not_guess_encoding: true})
end

#resultsObject

获取解析结果



32
33
34
# File 'lib/http_crawler/http/response.rb', line 32

def results
  @results ||= parsing
end

#validation_page?Boolean

是否验证码界面

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/http_crawler/http/response.rb', line 60

def validation_page?
  # 正则匹配数组 validations 的所有匹配值
  validations.each do |regular|
    regular_num = decoding_body =~ regular
    if regular_num
      Rails.logger.warn("触发验证信息")
      Rails.logger.warn(decoding_body[regular_num..(regular_num + 100)])
      return true
    end
  end
  return false
end