Class: GetProxyList::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/get_proxy_list/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(save_path = nil) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
# File 'lib/get_proxy_list/base.rb', line 9

def initialize(save_path=nil)
  @save_path = save_path
  unless @save_path
    @save_path = File.expand_path("../proxylist", __FILE__)
  end
end

Instance Attribute Details

#save_path=(value) ⇒ Object (writeonly)

To change this template use File | Settings | File Templates.



7
8
9
# File 'lib/get_proxy_list/base.rb', line 7

def save_path=(value)
  @save_path = value
end

Instance Method Details

#get_proxy_in_time_limit(limit, proxylist) ⇒ Object

获取符合时限的代理 limit 时限 proxylist 待筛选的代理列表



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
# File 'lib/get_proxy_list/base.rb', line 20

def get_proxy_in_time_limit(limit,proxylist)
  limit_list = []
  proxylist.each do |proxy|
    url = "http://" + proxy["ip"].to_s + ":" + proxy["port"].to_s
    time_start = Time.now.to_i
    begin
      timeout(limit+1) do
        doc = Nokogiri::HTML(open("http://www.baidu.com",:proxy=> url))
        x = doc.css("em")
      end
      time_end = Time.now.to_i
      time_use = time_end - time_start
      p  "#{url}   use_time:#{time_use}"
    rescue Exception =>e
      case e
        when Errno::ETIMEDOUT
          p "Use #{url} timeout"
        when Timeout::Error
          p "Use #{url} timeout"
        when Errno::ECONNREFUSED
          p "Use #{url} Error connection"
        else
          p "Use #{url} Error:#{e.to_s}"
      end
      time_use = -1
    end
    if(time_use>=0&&time_use<=limit)
      limit_list << url
    end
  end
  limit_list
end