Instead of opening, reading and closing the file all the time, you should read it once and then just use the string multiple times (this will also safe time and bandwidth).
img = f.read.scan(/<img/).length
This won't necessarily give you an accurate count of <img>
tags. For example it will also count occurrences of <img
that appear in comments or in scripts. If you want an accurate count, you should use an HTML parsing library. (Same applies to when you do the same thing to the other tags of course).
f.close
No need to return the stream when using open
with a block. The stream will be closed automatically when the block ends.
I'd also recommend to put the tag-counting code into a method. This way you don't need to repeat the same code for each tag.
If you're okay with changing the output, so that it's uniform for all tags, you might even turn the whole thing into a loop, so there's no repetition at all. Something like this:
%w(img a div h1 form).each do |tag|
count = contents.scan(/<#{tag}/).length
puts "\t#{count} #{tag} tags"
end