Pages

January 10, 2010

Python - Lookup Google PageRank Score

Here is a Python module for getting the PageRank of a site from Google. It uses the Google Toolbar 3.0.x/4.0.x Pagerank Checksum Algorithm.

The code is here:

pagerank.py

You can import it as a module and use its get_pagerank(url) method to lookup the PageRank of a URL from your Python code.

For example:

#!/usr/bin/env python

import pagerank

rank = pagerank.get_pagerank('http://www.google.com')
print rank

* note: only use this within Google's terms of service

12 comments:

  1. Try to avoid except Exception: (word pun intended).

    def get_pagerank(url):
    import urllib

    hsh = check_hash(hash_url(url))
    gurl='http://www.google.com/search?client=navclient-auto&' +\
    'features=Rank:&q=info:%s&ch=%s' % (urllib.quote(url), hsh)
    print gurl
    f = urllib.urlopen(gurl)
    rank = f.read()
    rank = rank.strip()
    rank = rank[9:]
    try:
    rank = int(rank)
    except ValueError:
    rank = 0

    rank = str(rank)

    return rank

    ReplyDelete
  2. Neat. I wanted to be able to call it from command line so I added:

    import sys

    and then replaced your "if name is main" with this:

    if __name__ == '__main__':
    if len(sys.argv)>1:
    site=sys.argv[1]
    else:
    site='http://www.google.com'

    pr=get_pagerank(site)
    print 'The page rank of %s is %s' % (site, pr)

    ReplyDelete
  3. Cool, thanks

    ReplyDelete
  4. AnonymousMay 27, 2010

    Thanks!

    Was easy to use. This is the first thing that I have ever run in Python.

    ReplyDelete
  5. Awesome. Just what I was looking for. Thanks for sharing!

    ReplyDelete
  6. This is very useful for me, thanks for shared.. :D

    ReplyDelete
  7. Thank you for publishing this function. Was just about to start analyzing the GoogleToolbar when I found your code repo at GoogleCode. Saves me bunches of time *grin*

    ReplyDelete
  8. Was easy to use. This is the first thing that I have ever run in Python.

    ReplyDelete
  9. Many thanks. That Python module is really handy.

    ReplyDelete
  10. Your code was returning a google error page so I reimplemented WWW:Google:PageRank perl module in python.

    Here is my solution for getting the google page rank. The script also supports getting the Alexa traffic rank.
    https://github.com/aablack/websearchapp/blob/master/search/rank_provider.py

    ReplyDelete
  11. @mrpringle,

    my code is a few years old, and not surprisingly goog changed something since.

    your rank_provider.py looks really nice. Are you going to package it for PyPI? If not, I'd be happy to help or do it for you. let me know

    -Corey

    ReplyDelete
  12. Hi Corey,

    Sounds like a good idea. I will look into it today.

    ReplyDelete