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

获取代理列表



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

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/grabepg/grab_base.rb', line 137

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型 代表秒



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/grabepg/grab_base.rb', line 70

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



125
126
127
128
129
130
131
132
133
# File 'lib/grabepg/grab_base.rb', line 125

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_list(path) ⇒ Object



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
# File 'lib/grabepg/grab_base.rb', line 33

def self.proxy_list(path)
  proxy_list = []
  crt_date = DateTime.now.strftime('%F')
  proxy_path = "%s/proxy/%s.txt" % [File.dirname(path),crt_date]
  p "Proxy_Path: #{proxy_path}"
  if File.exist?(proxy_path)
    file_proxy = File.open(proxy_path,"r")
    file_proxy.each_line {|line|
      proxy_list << line.chomp.to_s
    }
    p "Get Proxy_list:#{proxy_list}"
    file_proxy.flush
    file_proxy.close
  else
    proxy_list=GetProxyList.get_list(ENV["proxy_limit"].to_i,ENV["proxy_page"].to_i)
    dirpath = "#{File.dirname(path)}/proxy/"
    Dir.open(dirpath)  {|fna|
      fna.each do |fn|
        if(fn.to_s != ".." && fn.to_s != ".")
          File.delete("#{dirpath + fn.to_s}")
        end
      end
    }
    file_proxy = File.new(proxy_path,"a")
    proxy_list.each do |proxy|
      p "Proxy:#{proxy}"
      file_proxy.puts proxy
    end
    file_proxy.flush
    file_proxy.close
  end
  return proxy_list
end

Instance Method Details

#conversion_what_day(whatday) ⇒ Object



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

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/grabepg/grab_base.rb', line 158

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<4
  unless @no_firest<4
    @no_firest=0
    raise RuntimeError,"Error: #{err}"
  end
  doc
end

#get_doc_with_proxy(proxylist, url) ⇒ Object

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/grabepg/grab_base.rb', line 183

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%@size
      return doc
    end
    @proxyindex += 1
    @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