使用 Ruby 获取网页内容——我遇到了麻烦 [英] Getting webpage content with Ruby -- I'm having troubles

查看:36
本文介绍了使用 Ruby 获取网页内容——我遇到了麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个*页面中删除内容.我查找的所有内容都提供了解析 CSS 元素的解决方案;但是,那个页面没有.

I want to get the content off this* page. Everything I've looked up gives the solution of parsing CSS elements; but, that page has none.

这是我发现的唯一看起来应该可以工作的代码:

Here's the only code that I found that looked like it should work:

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 中的下划线 (_).

If you try to format this as a link in the post it doesn't recognize the underscore (_) in the URL for some reason.

推荐答案

您确实想使用内核类提供的 open(),它可以从 URI 中读取您只需要 OpenURI 库第一:

You really want to use open() provided by the Kernel class which can read from URIs you just need to require the OpenURI library first:

require 'open-uri'

像这样使用:

require 'open-uri'
file = open('http://hiscore.runescape.com/index_lite.ws?player=zezima')
contents = file.read
puts contents

这个相关的 SO 线程涵盖了相同的问题:

This related SO thread covers the same question:

从本地文件打开 IO 流或网址

这篇关于使用 Ruby 获取网页内容——我遇到了麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆