views:

40

answers:

2

I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error:

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

I've tried to rescue it, but I can't to make it work. Can anyone help? Thanks

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

I also need help rescuing another error:

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

Thanks.

A: 

Double quotes inside the other double quotes! Use single quotes for the call to at_css():

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

Turns out a simple rescue StandardError did the trick.

Senthil