Ruby:使用open()和NET :: HTTP模块获取Web内容有什么区别?
发布时间:2020-12-17 02:22:01 所属栏目:百科 来源:网络整理
导读:参见英文答案 What is the difference between Ruby’s ‘open-uri’ and ‘Net:HTTP’ gems?????????????????????????????????????2个 一个单独的SO帖子提供了不同的方法来获取Ruby中的Web内容,但并没有完全解释为什么一个优于另一个. 使用open()和NET :: H
参见英文答案 >
What is the difference between Ruby’s ‘open-uri’ and ‘Net:HTTP’ gems?????????????????????????????????????2个
一个单独的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) 解决方法
经验法则:尽可能使用
OpenURI .
原因是 另一方面,如果你想要一些OpenURI没有给你的低级功能,更喜欢 正如official documentation所述:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |