(RUBY)如何阅读HTML标签内容并在控制台中打印它们

时间:2014-05-24 11:11:20

标签: html ruby tags

我说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

1 个答案:

答案 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