我想从这个*页面中获取内容。我查找的所有内容都提供了解析 CSS 元素的解决方案;但是,那个页面没有。
这是我发现的唯一看起来应该有效的代码:
file = File.open('http://hiscore.runescape.com/index_lite.ws?player=zezima', "r")
contents = file.read
puts contents
错误:
tracker.rb:1:in 'initialize': Invalid argument - http://hiscore.runescape.com/index_lite.ws?player=zezima (Errno::EINVAL)
from tracker.rb:1:in 'open'
from tracker.rb:1
*http://hiscore.runescape.com/index_lite.ws?player=zezima
如果您尝试将其格式化为帖子中的链接,由于某种原因它无法识别 URL 中的下划线 (_)。
最佳答案
您真的想使用内核类提供的 open(),它可以从 URI 中读取您只需要首先需要 OpenURI 库:
require 'open-uri'
这样使用:
require 'open-uri'
file = open('http://hiscore.runescape.com/index_lite.ws?player=zezima')
contents = file.read
puts contents
这个相关的 SO 线程涵盖了相同的问题:
关于ruby - 使用 Ruby 获取网页内容——我遇到了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854207/