Skip to content

Instantly share code, notes, and snippets.

@kozo002
Created January 6, 2012 05:00
Show Gist options
  • Select an option

  • Save kozo002/1569077 to your computer and use it in GitHub Desktop.

Select an option

Save kozo002/1569077 to your computer and use it in GitHub Desktop.
Get <title> element text using Nokogiri
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'nkf'
def get_title(url)
url.gsub!(Regexp.new("[^#{URI::PATTERN::ALNUM}\/\:\?\=&~,\.\(\)#]")) {|match| ERB::Util.url_encode(match)}
read_data = NKF.nkf("--utf8", open(url).read)
Nokogiri::HTML.parse(read_data, nil, 'utf8').xpath('//title').text
end
p get_title 'http://example.com/hoge/huga'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment