HTTPでGET その2
Posted feedbacks - Ruby
Net::HTTP::Proxy を利用する
1 2 3 4 5 6 7 8 9 10 | require 'net/http'
def get_with_proxy(proxy_server, proxy_port)
proxy = Net::HTTP::Proxy(proxy_server, proxy_port)
http = proxy.new('ja.doukaku.org')
http.open_timeout = 1
http.start do |h|
response = h.get('/feeds/comments/')
puts response.body
end
end
|
WWW::Mechanizeで.
うちの環境では,timeout 1秒だと成功するorzので,0.1秒にしました.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/usr/local/bin/ruby
require 'rubygems'
require 'mechanize'
class Crawler < WWW::Mechanize
attr_accessor :agent
PROXY_HOST = 'localhost'
PROXY_PORT = '8080'
URI = 'http://ja.doukaku.org/feeds/comments/'
def initialize(conf = {})
super()
self.max_history = 1 # 幸せになるおまじない
self.set_proxy(PROXY_HOST, PROXY_PORT)
conf.each {|method, value| self.send(method, value) }
end
def fetch
get(URI)
end
end
# puts Crawler.new.fetch.body # => succeed
puts Crawler.new({:open_timeout= => 0.1, :read_timeout= => 0.1}).fetch.body
|


ところてん
#4798()
Rating2/2=1.00
see: HTTPでGET
[ reply ]