2010-06-15 86 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}"