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:

Dimitris Leventeas said...

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

Douglas Sims said...

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)

Anonymous said...

Cool, thanks

Anonymous said...

Thanks!

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

Dustin said...

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

hiu said...

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

Alvin Mites said...

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*

Inversiones en oro said...

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

Alex said...

Many thanks. That Python module is really handy.

mrpringle said...

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

Corey Goldberg said...

@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

mrpringle said...

Hi Corey,

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