Get title from URL?

Anybody have a code snippet that extracts the title from the <title> tag
from a given URL?

···

--
Posted via http://www.ruby-forum.com/.

require 'Hpricot'
require 'open-uri'

url="http://www.ruby-lang.org/"
doc=Hpricot(open(url))
title=(doc/"title").inner_text

=> "Ruby Programming Language"

require 'rubygems'
require 'mechanize'
title = WWW::Mechanize.new.get('http://google.com').title
=> "Google"

Regards,
Park Heesob

···

2009/4/24 Cisco Ri <cisco.riordan@gmail.com>:

Anybody have a code snippet that extracts the title from the <title> tag
from a given URL?

Cisco Ri schrieb:

Anybody have a code snippet that extracts the title from the <title> tag
from a given URL?

Without installing special things:

require 'open-uri'
open('http://google.com/&#39;\).read =~ /<title>(.*?)<\/title>/
p $1

Thanks, both work great.

···

--
Posted via http://www.ruby-forum.com/.

Heesob Park wrote:

Anybody have a code snippet that extracts the title from the <title> tag
from a given URL?

require 'rubygems'
require 'mechanize'
title = WWW::Mechanize.new.get('http://google.com').title
=> "Google"

Regards,
Park Heesob

I used this method for a while, and it was fine for most sites.
However, with wikipedia.org it errored out with a 403 Forbidden error.
The Hpricot/open-uri method works for most sites, including
wikipedia.org, but for thesixtyone.com (Javascript intensive site) it
errors out with a 500 Internal Server error.

I haven't tried out the open-uri only method yet.

Thanks for the help everyone.

···

2009/4/24 Cisco Ri <cisco.riordan@gmail.com>:

--
Posted via http://www.ruby-forum.com/\.

You can work around like this:

require 'rubygems'
require 'mechanize'
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac Safari'
title = agent.get('http://wikipedia.org').title

Regards,
Park Heesob

···

2009/4/28 Cisco Ri <cisco.riordan@gmail.com>:

Heesob Park wrote:

2009/4/24 Cisco Ri <cisco.riordan@gmail.com>:

Anybody have a code snippet that extracts the title from the <title> tag
from a given URL?

require 'rubygems'
require 'mechanize'
title = WWW::Mechanize.new.get('http://google.com').title
=> "Google"

Regards,
Park Heesob

I used this method for a while, and it was fine for most sites.
However, with wikipedia.org it errored out with a 403 Forbidden error.
The Hpricot/open-uri method works for most sites, including
wikipedia.org, but for thesixtyone.com (Javascript intensive site) it
errors out with a 500 Internal Server error.