Knowledge Base

October 18, 2007

Using Hpricot to fetch the title of the url passed

Filed under: Technical — rkutti @ 9:47 am

When surfing the net I found Hpricot for Ruby.This helps me in parsing the HTML pages.
To install ‘hpricot’ use gem installer..
In the command prompt type this to install ‘hpricot’
gem install hpricot

Then in your code include ‘hpricot’ using

require ‘hpricot’
require ‘open-uri’

@title = (Hpricot(open(url))/:title).inner_text

This code will help in fetching the title of the html pages.

The code under my prevoius post ‘Get the title of the given url using ruby’ can be replaced with this code. This reduces the number of lines of code.

I got this code from this link
The usage of Hpricot doesn’t stop with this. It is used in many cases, i will post usages of ‘hrpicot’ in future.

October 8, 2007

BumpedINTO.com

Filed under: Technical — rkutti @ 7:46 am

BumpedInto is a Social Profile Bookmarking application which helps you in achieving your goals.

You can take a quick look of the tour to know more about this bookmarking application.

Tour

Click here to Sign-Up

September 21, 2007

To get the favicon from the url using ruby

Filed under: Technical — rkutti @ 1:37 pm

The following ruby code helps us to get the favicon from the given url:

For this I have written a icon.rhtml file which contains the input field to get the url from the user

icon.rhtml

<form>
URL: <input type=”text” name=”main_url” />
<br/>
Favicon URI: <%=@url%>
<br/>
Favicon: <img src= <%=@url%> style=”width: 16px; height: 16px; border: 0; vertical-align: -4px;” alt=”">
</form>

in the corresponding controller file icon_controller.rb

def icon
require ‘open-uri’
begin
main_url = params[:main_url];
f = open(main_url)
rescue
err = $!
err_s = err.to_s
end
if err_s == nil
a = f.read
s= a.slice(/link.*rel\=.*shortcut.*\”/)
if s != nil
exp = s.split(“href=”)
exp1 = exp[1].split(‘”‘)
exp2 = exp1[1].split(“http:”)
if exp2[0] != “”
@url = main_url+exp1[1]
else
@url = exp1[1]
end
else
@url = main_url+”/favicon.ico”
end

else

end

end

September 4, 2007

Get the title of the given url using ruby

Filed under: Technical — rkutti @ 12:59 pm

We can use the following ruby code snippets to get the title of given url:

require ‘open-uri’

f = open(“http://www.google.com”)

a = f.read

s = a.split(“title>”)

url = s[1]

results in:

Google

« Newer Posts

Blog at WordPress.com.