【问题标题】:404 not found, but can access normally from web browser404未找到,但可以从浏览器正常访问
【发布时间】:2014-10-30 18:31:18
【问题描述】:

我在这方面尝试了很多网址,但在我遇到这个特定的网址之前,它们似乎都很好:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))
puts doc

这是结果:

/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in `open_http': 404 Not Found (OpenURI::HTTPError)
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:709:in `buffer_open'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:689:in `open'
    from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:34:in `open'
    from test.rb:5:in `<main>'  

我可以从网络浏览器访问它,但我根本不明白。

发生了什么,我该如何处理这种错误?我可以忽略它,让其他人做他们的工作吗?

【问题讨论】:

  • 您使用的是 Ruby 2+,因此没有必要使用 require 'rubygems'。该要求在 Ruby 1.9 中消失了。

标签: ruby http-status-code-404 nokogiri open-uri


【解决方案1】:

您将收到404 Not Found (OpenURI::HTTPError),因此,如果您想允许您的代码继续运行,请针对该异常进行救援。像这样的东西应该可以工作:

require 'nokogiri'
require 'open-uri'

URLS = %w[
  http://www.moxyst.com/fashion/men-clothing/underwear.html
]

URLs.each do |url|
  begin
    doc = Nokogiri::HTML(open(url))
  rescue OpenURI::HTTPError => e
    puts "Can't access #{ url }"
    puts e.message
    puts
    next
  end
  puts doc.to_html
end

您可以使用更通用的异常,但随后会遇到奇怪的输出问题,或者可能会以导致更多问题的方式处理不相关的问题,因此您需要确定所需的粒度。

您甚至可以嗅探 HTTPd 标头、响应的状态,或者如果您想要更多控制权并且想要对 401 或 404 执行不同的操作,则可以查看异常消息。

我可以从网络浏览器访问它,但我根本不明白。

嗯,这可能是服务器端发生的事情:也许他们不喜欢您发送的 UserAgent 字符串? OpenURI documentation 显示了如何更改该标题:

可以通过可选的散列参数指定额外的标头字段。

open("http://www.ruby-lang.org/en/",
  "User-Agent" => "Ruby/#{RUBY_VERSION}",
  "From" => "foo@bar.invalid",
  "Referer" => "http://www.ruby-lang.org/") {|f|
  # ...
}

【讨论】:

    【解决方案2】:

    您可能需要将“User-Agent”作为参数传递给 open 方法。有些网站需要有效的用户代理,否则它们根本不会响应或显示 404 not found 错误。

    doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html", "User-Agent" => "MyCrawlerName (http://mycrawler-url.com)"))
    

    【讨论】:

      【解决方案3】:

      那么发生了什么,我该如何处理这种错误。

      不知道发生了什么,但您可以通过捕获错误来处理它。

      begin
        doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))
        puts doc
      rescue => e
        puts "I failed: #{e}"
      end
      

      我可以忽略它,让其他人做他们的工作吗?

      当然!可能是?不确定。我们不知道您的要求。

      【讨论】:

      • 但发生在我身上的是next is invalid
      猜你喜欢
      • 2019-05-04
      • 2011-02-22
      • 2011-05-10
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2016-09-02
      相关资源
      最近更新 更多