ruby: ruby script to get Alexa global rank for free

Trabla: ruby: ruby script to get Alexa global rank for free


ruby script to get Alexa global rank - tutorial
Alexa Internet, Inc. is a California-based company that provides commercial web traffic data and analytics. It is a wholly owned subsidiary of Amazon.com.

Founded as an independent company in 1996, Alexa was acquired by Amazon in 1999. Its toolbar collects data on browsing behavior and transmits them to the Alexa website, where they are stored and analyzed, forming the basis for the company's web traffic reporting. According to its website, Alexa provides traffic data, global rankings and other information on 30 million websites, and as of 2015 its website is visited by over 6.5 million people monthly.
https://en.wikipedia.org/wiki/Alexa_Internet

Alexa web traffic metrics are available via  API at http://aws.amazon.com/awis
and is paid service:

This example shows how to get alexa global rank for free, by parsing free page data.

Solving:

 1. Install nokogiri gem for parsing html
http://www.nokogiri.org/tutorials/installing_nokogiri.html

gem install nokogiri
Fetching: mini_portile2-2.0.0.gem (100%)
Successfully installed mini_portile2-2.0.0
Fetching: nokogiri-1.6.7.2-x64-mingw32.gem (100%)
Nokogiri is built with the packaged libraries: libxml2-2.9.2, libxslt-1.1.28, zl
ib-1.2.8, libiconv-1.14.
Successfully installed nokogiri-1.6.7.2-x64-mingw32
Parsing documentation for mini_portile2-2.0.0
Installing ri documentation for mini_portile2-2.0.0
Parsing documentation for nokogiri-1.6.7.2-x64-mingw32
Installing ri documentation for nokogiri-1.6.7.2-x64-mingw32
Done installing documentation for mini_portile2, nokogiri after 6 seconds
2 gems installed


2. Ruby script to get Alexa global rank value

require 'nokogiri'
require 'open-uri'


def get_alexa_global_rank( site_url )

    alexa_url = "http://www.alexa.com/siteinfo/"
    url = alexa_url + site_url

       # Fetch and parse HTML document
    doc = Nokogiri::HTML(open( url ))

    # Xpath query to get Alexa global rank value
    xpath_query = '//*[@id="traffic-rank-content"]/div/span[2]/div[1]/span/span/div/strong/text()'
   
    # Execute xpath query over html document
    global_rank = doc.xpath( xpath_query )

       return global_rank

end

# Usage example

target_url = 'stackoverflow.com'

puts get_alexa_global_rank( target_url )


Result: 59

ruby script to get Alexa global rank - tutorial screenshot





No comments:

Post a Comment