【问题标题】:rescue Nokogiri error救援 Nokogiri 错误
【发布时间】:2010-06-15 19:05:20
【问题描述】:

我有一个简单的脚本,它查看 Twitter 用户名并获取我的位置。但是有些用户名不存在,我收到错误:

/usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError)

我试图拯救它,但我无法让它发挥作用。任何人都可以帮忙吗?谢谢

a = []
my_file = File.new("location.txt", 'a+')

File.open('address.txt', 'r') do |f|
while line = f.gets

 url = "http://twitter.com/#{line}"
 doc = Nokogiri::HTML(open(url, 'User-Agent' => 'ruby'))
 doc.css("#side #profile").each do |loc|
   my_file.puts "http://twitter.com/#{line} #{loc.at_css(".adr").text}"
   puts line
 end
 end
end

我还需要帮助来挽救另一个错误:

twitter.rb:14: undefined method `text' for nil:NilClass (NoMethodError)

谢谢。

【问题讨论】:

    标签: ruby-on-rails nokogiri rescue


    【解决方案1】:

    其他双引号内的双引号!对 at_css() 的调用使用单引号:

    my_file.puts "http://twitter.com/#{line} #{loc.at_css('.adr').text}"
    

    【讨论】:

      【解决方案2】:

      事实证明,一个简单的救援 StandardError 成功了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        • 1970-01-01
        • 2011-03-11
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多