单独的SO帖子提供了用于获取Ruby中Web内容的不同方法,但并未完全解释为什么一个方法比另一个方法更受欢迎。
如下所示,使用open()和NET :: HTTP模块来获取Web内容有什么区别?为什么NET :: HTTP被认为是“更好”的方法?
**open() 1:**
require 'open-uri'
file = open('http://hiscore.runescape.com/index_lite.ws?player=zezima')
contents = file.read
**open() 2:**
require 'open-uri'
source = open('http://www.google.com', &:read)
**NET::HTTP 1:**
require 'uri'
require 'net/http'
url = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
315 次 |
| 最近记录: |