我正在尝试阅读Stanford ecorner XML:
open("http://ecorner.stanford.edu/RecentlyAdded.xml")
但遇到以下错误消息:
OpenURI::HTTPError: 500 Internal Server Error
from /usr/local/lib/ruby/1.8/open-uri.rb:277:in `open_http'
from /usr/local/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
from /usr/local/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from /usr/local/lib/ruby/1.8/open-uri.rb:162:in `catch'
from /usr/local/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
from /usr/local/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from /usr/local/lib/ruby/1.8/open-uri.rb:518:in `open'
from /usr/local/lib/ruby/1.8/open-uri.rb:30:in `open'
from (irb):65
from :0
我相信,但我错了,这是因为我需要登录才能使用Feed。
我可以使用的任何解决方法吗?
答案 0 :(得分:2)
如果没有登录,您应该获得401 Unauthorized
而不是500
的HTTP响应代码。我试图在浏览器中打开网站,这有效。结果他们的网络服务器不喜欢缺少用户代理,所以如果你添加open-uri
工作:
>> require 'open-uri'
#=> true
>> open("http://ecorner.stanford.edu/RecentlyAdded.xml", 'User-Agent' => 'ruby')
#=> #<File:/var/folders/H9/H9qnar1yGZqBrWFGuTE0RU+++TI/-Tmp-/open-uri20110505-25566-zsc3pd-0>
答案 1 :(得分:0)
这对我有用:
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::XML(open('http://ecorner.stanford.edu/RecentlyAdded.xml'))
puts doc.search('title').map{ |n| n.text }
>> Recently Added STVP Entrepreneurship Corner Materials
>> STVP Entrepreneurship Corner
>> Podcast: Developing Products that Save Lives - Richard Scheller (Genentech)
>> Podcast: How to Build Instant Connections - Ori Brafman (Author)
>> Podcast: A New Vision for Capital Markets - Barry Silbert (SecondMarket)
>> Podcast: Effective Models for Sustainable Growth - Jennifer Morris (Conservation International)
请注意,您有500个范围的错误。这意味着他们的服务器正在运行,但功能足以承认问题。如果你有一个400范围的错误,他们会因某些原因拒绝你访问这些内容,所以我怀疑问题是身份验证还是你身边的任何事情。