2010-06-15 58 views
0

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

/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) 

感谢。

回答

0

原来简单的救援StandardError的奏效了。

1

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

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