【发布时间】:2015-02-09 01:21:32
【问题描述】:
我有一个我一直在研究的简单的 html 解析器(用于学习目的)。:
require 'open-uri'
puts "Enter URL to parse HTML: "
url = gets.chomp
puts "Enter tag to parse from: "
tag = gets.chomp
response = open(url).read
title1 = response.index(tag)
title2 = response.index(tag.insert(1,'/')) -1
result = response[(title1 + tag.length - 1)..title2]
print result
当我输入 http://twitter.com 时,我收到以下错误消息:
ERROR: `open_loop': redirection forbidden: http://twitter.com -> https://twitter.com/ (RuntimeError)
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from /home/ubuntu/workspace/htmlparse.rb:6:in `<main>'
有什么建议或帮助吗?我是 Ruby 新手,我知道其他 html 解析模块,但我这样做是为了学习 Ruby 基础知识。谢谢。
【问题讨论】:
-
我相信这是因为 twitter 使用了
https。 FWIW - 如果您只是想学习和闲逛,您可能想访问http://www.example.org之类的网站而不是 twitter。 -
我知道
http://网站可以工作,但是我认为open-uri会自动重定向到https,https://twitter.com可以,但是http不行,有什么解决办法吗? -
我的建议:将该文件下载到您控制的某个服务器(github、bintray),这样它就可以避免重定向(抱歉不得不输入)