# Get title from URL?

**URL:** <https://rubytalk.org/t/get-title-from-url/53025>\
**Category:** ruby-talk\
**Created:** [24 April 2009 08:03 UTC](https://rubytalk.org/t/get-title-from-url/53025 "2009-04-24T08:03:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cisco\_Ri](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Cisco\_Ri](https://rubytalk.org/u/Cisco_Ri)\
**Post date:** [24 April 2009 08:03 UTC](https://rubytalk.org/t/get-title-from-url/53025/1 "2009-04-24T08:03:29Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dan6](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dan6](https://rubytalk.org/u/Dan6)\
**Post date:** [24 April 2009 08:14 UTC](https://rubytalk.org/t/get-title-from-url/53025/2 "2009-04-24T08:14:06Z")

</div>

require 'Hpricot'  
require 'open-uri'

url="[http://www.ruby-lang.org/](http://www.ruby-lang.org/)"  
doc=Hpricot(open(url))  
title=(doc/"title").inner\_text

=\> "Ruby Programming Language"

---

<div class="post-metadata">

**Author:** ![Heesob\_Park](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Heesob\_Park](https://rubytalk.org/u/Heesob_Park)\
**Post date:** [24 April 2009 08:27 UTC](https://rubytalk.org/t/get-title-from-url/53025/3 "2009-04-24T08:27:07Z")

</div>

require 'rubygems'  
require 'mechanize'  
title = WWW::Mechanize.new.get('[http://google.com](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?

---

<div class="post-metadata">

**Author:** ![Rudiger\_Bahns](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@Rudiger\_Bahns](https://rubytalk.org/u/Rudiger_Bahns)\
**Post date:** [27 April 2009 12:05 UTC](https://rubytalk.org/t/get-title-from-url/53025/4 "2009-04-27T12:05:04Z")

</div>

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;\](http://google.com/&#39;%5C)).read =~ /\<title\>(.\*?)\<\/title\>/  
p $1

---

<div class="post-metadata">

**Author:** ![Cisco\_Ri](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Cisco\_Ri](https://rubytalk.org/u/Cisco_Ri)\
**Post date:** [24 April 2009 08:31 UTC](https://rubytalk.org/t/get-title-from-url/53025/5 "2009-04-24T08:31:56Z")

</div>

Thanks, both work great.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Cisco\_Ri](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Cisco\_Ri](https://rubytalk.org/u/Cisco_Ri)\
**Post date:** [27 April 2009 15:29 UTC](https://rubytalk.org/t/get-title-from-url/53025/6 "2009-04-27T15:29:35Z")

</div>

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](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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Heesob\_Park](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@Heesob\_Park](https://rubytalk.org/u/Heesob_Park)\
**Post date:** [28 April 2009 00:54 UTC](https://rubytalk.org/t/get-title-from-url/53025/7 "2009-04-28T00:54:52Z")

</div>

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](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](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.
