Class: Grabepg::GrabBase

Inherits:
Object
  • Object
show all
Defined in:
lib/grabepg/grab_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_proxy_listObject

获取代理列表



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

def self.get_proxy_list()
  list = gg('http://www.proxycn.cn/html_proxy/30fastproxy-1.html')
  if list.count ==0
    list = gg('http://www.proxycn.cn/html_proxy/http-1.html')
  end
  ips_ports = []
  regex_port = /(?<=<TD class="list">)[0-9]*?(?=<\/TD>)/
  regex_ip   = /(?<=a href\=whois.php\?whois\=)[0-9,.]*/
  list.each do |proxy_txt|
    port = proxy_txt[regex_port]
    ip = proxy_txt[regex_ip]
    if(ip != ""&& !port.to_s.eql?('3128'))
      port_ip = ip.to_s + ":" + port.to_s
      ips_ports << port_ip
    end
  end
  p "Count: #{ips_ports.count}"
  ips_ports
end

.get_proxylist_dianxinObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/grabepg/grab_base.rb', line 107

def self.get_proxylist_dianxin()
  list = gg("http://www.proxycn.cn/countryDX.php")
  if list.count ==0
    list = gg('http://www.proxycn.cn/html_proxy/http-1.html')
  end
  ips_ports = []
  regex_port = /(?<=<TD class="list">)[0-9]*?(?=<\/TD>)/
  regex_ip   = /(?<=a href\=whois.php\?whois\=)[0-9,.]*/
  list.each do |proxy_txt|
    port = proxy_txt[regex_port]
    ip = proxy_txt[regex_ip]
    if(ip != ""&& !port.to_s.eql?('3128'))
      port_ip = ip.to_s + ":" + port.to_s
      ips_ports << port_ip
    end
  end
  p "Count: #{ips_ports.count}"
  ips_ports
end

.get_topfast_list(use_time) ⇒ Object

获取指定访问速度的代理服务器time为最慢速度的时间 int型 代表秒



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
69
70
71
72
# File 'lib/grabepg/grab_base.rb', line 40

def self.get_topfast_list(use_time)
  fast_list = []
  time_use = 0
  ips_ports = get_proxy_list()
  ips_ports = get_proxylist_dianxin if ips_ports.size==0
  ips_ports.each do |ip_port|
    time_start = Time.now.to_i
    begin
      timeout(use_time) do
        doc = Nokogiri::HTML(open("http://www.baidu.com",:proxy=> "http://#{ip_port}"))
      end
      time_end = Time.now.to_i
      time_use = time_end - time_start
      p  "http://#{ip_port}   use_time:#{time_use}"
    rescue Exception =>e
      case e
        when Errno::ETIMEDOUT
          p "Use http://#{ip_port} timeout"
        when Timeout::Error
          p "Use http://#{ip_port} timeout"
        when Errno::ECONNREFUSED
          p "Use http://#{ip_port} Error connection"
        else
          p "Use http://#{ip_port} Error:#{e.to_s}"
      end
      time_use = -1
    end
    if(time_use > 0 &&time_use < 8)
      fast_list << ip_port
    end
  end
  fast_list
end

.gg(url) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/grabepg/grab_base.rb', line 95

def self.gg(url)
  regex_list = /<TD class="list">.*<\/TD>/
  href =URI.parse(url)
  contxt = ""
  href.open{ |f|
    f.each_line {|line| contxt =contxt + line + "\n"}
  }
  list = contxt.scan(regex_list)
end

.proxy_limit(limit, proxy_list) ⇒ Object



33
34
35
# File 'lib/grabepg/grab_base.rb', line 33

def self.proxy_limit(limit,proxy_list)
  return GetProxyList.get_proxy_in_time_limt(limit,proxy_list)
end

Instance Method Details

#conversion_what_day(whatday) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grabepg/grab_base.rb', line 12

def conversion_what_day(whatday)
  ret = "星期"
  case whatday.to_i
    when 1
      ret += ""
    when 2
      ret += ""
    when 3
      ret += ""
    when 4
      ret += ""
    when 5
      ret += ""
    when 6
      ret += ""
    when 0
      ret += ""
  end
  ret
end

#err_doc_proxy(proxy, proxylist, url = "", err = "") ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/grabepg/grab_base.rb', line 128

def err_doc_proxy(proxy,proxylist,url="",err="")
  if proxy.empty?||proxy.nil?
    proxylist.delete_at[@proxyindex]
  end


  unless @no_firest
    @no_firest = 0
  end

  @no_firest += 1
  p "*************************Proxy:#{proxy}, url:#{url} Error:#{err}"
  #proxylist.delete(proxy)    #删除出错的代理  但如果是此网页错误则会引起BUG待修复
  @proxyindex += 1
  @proxyindex=@proxyindex%@size
  doc=get_doc_with_proxy(proxylist,url) if @no_firest<10
  unless @no_firest<10
    @no_firest=0
    raise RuntimeError,"Error: #{err}"
  end
  doc
end

#get_doc_with_proxy(proxylist, url) ⇒ Object

使用代理获取url的html的doc值



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/grabepg/grab_base.rb', line 153

def get_doc_with_proxy(proxylist,url)
  unless proxylist.nil?||proxylist.empty?
    unless @proxyindex
      @proxyindex = 0
    end
    @size = proxylist.size
    @proxyindex=@proxyindex%proxylist.size
    if(proxylist[@proxyindex])
      proxy = proxylist[@proxyindex]
    else
      proxy = proxylist[@proxyindex+1]
    end
    begin
      ic = Iconv.new("UTF-8//IGNORE","GB2312")
      doc = Nokogiri::HTML(ic.iconv(open(url,:proxy=>"#{proxy}").read)) unless proxy.nil?||proxy.empty?
      if doc.nil?
      	p "DOC is nil"
        doc=err_doc_proxy(proxy,proxylist,url,"doc nil")
        @no_firest=0
      end
      @no_firest = 0
    rescue => err
      p "IN Rescue"
      doc=err_doc_proxy(proxy,proxylist,url,err.to_s)
      @no_firest=0
      p "Get DOC"
      @proxyindex += 1
      @proxyindex = @proxyindex + Time.now.to_i
      @proxyindex=@proxyindex%@size
      return doc
    end
    @proxyindex += 1
    @proxyindex = @proxyindex + Time.now.to_i
    @proxyindex=@proxyindex%@size
  else
    begin
      ic = Iconv.new("GB2312//IGNORE","GB2312")
      doc = Nokogiri::HTML(ic.iconv(open(url).read)) if proxy.nil?||proxy.empty?
    rescue => err
      p "Error : Proxy:#{proxy}, url:#{url}"
      raise RuntimeError,"Error: #{err.to_s} Method:get_doc_with_proxy"
    end
  end
  doc
end