我说3天前弄乱了Ruby,我想找到标签的内容
这是我到目前为止的代码。有人可以帮忙吗?
require 'open-uri'
print "Enter a website domain you like (without the http://): "
website = gets.chomp
if website.count(".") >= 2
first_letter = website.index(".") + 1 # we want the letter just after the first dot
after_first_letter = first_letter + 1
last_letter = (website.index(".", after_first_letter) - 1)
website_title = website[first_letter..last_letter]
elsif website.count(".") == 1
website_title = website[0..(website.index(".") - 1)]
else
website_title = nil
end
unless website_title.nil?
file = open("http://#{website}")
contents = file.read
title = contents.index("<h3>")
p "This is the title: #{title}"
else
puts "We aren't sure what you did. Try again..."
end
答案 0 :(得分:0)
使用nokogiri
解析html。运行gem install nokogiri
。
require 'nokogiri'
html = Nokogiri::HTML(open("http://#{website}"))
html.css('h3').each do |title_node|
puts "Title: #{title_node.content}"
end