论坛首页 Ruby版 rails

rails 中如何直接获得某个url的内容

浏览 986 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2007-01-18 关键字: 内容抓取
在rails中怎么直接读取其他网站上的网页内容?
   
时间:2007-01-18
http://www.ruby-doc.org/core/classes/Net/HTTP.html

Examples
Getting Document From WWW Server

Example 1: Simple GET+print

require 'net/http'
Net::HTTP.get_print 'www.example.com', '/index.html'

Example 2: Simple GET+print by URL

require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.example.com/index.html')

Example 3: More generic GET+print

require 'net/http'
require 'uri'

url = URI.parse('http://www.example.com/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/index.html')
}
puts res.body

Example 4: More generic GET+print

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
puts res.body
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐