Class: GetProxyList::FromProxyCn

Inherits:
Object
  • Object
show all
Includes:
GetProxyList
Defined in:
lib/get_proxy_list/from_proxy_cn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GetProxyList

get_list, get_proxy_in_time_limt, proxy_list

Constructor Details

#initializeFromProxyCn

Returns a new instance of FromProxyCn.



10
11
12
# File 'lib/get_proxy_list/from_proxy_cn.rb', line 10

def initialize
  @site = "http://www.cnproxy.com"
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'lib/get_proxy_list/from_proxy_cn.rb', line 8

def site
  @site
end

Instance Method Details

#_getproxylist(url) ⇒ Object

获取代理列表



16
17
18
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
# File 'lib/get_proxy_list/from_proxy_cn.rb', line 16

def _getproxylist(url)
  p " Url : #{url}"
  begin
    hash_port = {"c"=>"1","a"=>"2","z"=>"3","m"=>"4","b"=>"5","w"=>"6","i"=>"7","x"=>"8","l"=>"9","f"=>"0"}
    html = open(url).read
    html.force_encoding("gbk")
    html.encode!("utf-8", :undef => :replace, :replace => "", :invalid => :replace)
    doc=Nokogiri::HTML.parse html
    #doc = Nokogiri::HTML(open(url))
    proxylist = []
    doc.css('table').each do |_table|
      _table.css('tr').each do |tr|
        if !tr.css('td')[0].content.eql?("IP:Port")&&!tr.css('td')[0].content.empty?
          proxy=tr.css('td')[0].content
          p "Proxy: #{proxy[1]}"
          proxy = proxy.split("document.write")
          _proxy = proxy[1].split(":")
          port = _proxy[1].gsub(")","")
          p "port : #{port}"
          port=port.split("+")
          _port = ""
          1.upto(port.length-1).each do |index|
           p "Hash:#{hash_port[port[index]]}"
          	unless hash_port[port[index]].nil?
            	_port+=hash_port[port[index]]
            else
              _port=8080 
              break
            end
          end
          proxylist << {"ip"=>proxy[0],"port"=>_port}
        end
      end
    end
    p "ProxyList: #{proxylist}"
    return proxylist
    #rescue => err
    #  raise "Get Proxy list Error! Class:'FromProxyCn' Gem:'get_proxy_list' Error:'#{err.to_s}'"
  end
end

#get_proxylist(page) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/get_proxy_list/from_proxy_cn.rb', line 69

def get_proxylist(page)
  urls = get_url_list(page)
  proxylist = []
  urls.each do |url|
    proxylist =proxylist+proxylist|_getproxylist(url)
  end
  return proxylist
end

#get_url_list(page) ⇒ Object

根据需要的页数获取URL列表 page 将获取的页数



60
61
62
63
64
65
66
# File 'lib/get_proxy_list/from_proxy_cn.rb', line 60

def get_url_list(page)
  urls = []
  1.upto(page.to_i).each do |_page|
    urls << @site+"/proxy#{_page.to_s}.html"
  end
  return urls
end