Using Google Search from your Python code [fixed]

Just a quick and dirty fix IMHO the best python google API written by Mario Vilas from breakingcode blog.

Check diff-style output modifications below:

--- google-mario.py     2010-11-21 00:59:42.000000000 -0200
+++ google-thebug.py    2010-11-21 01:00:00.000000000 -0200
@@ -138,9 +138,12 @@
soup = BeautifulSoup.BeautifulSoup(html)
anchors = soup.findAll('a')
for a in anchors:
-            link = a['href']
+            try:
+                link = a['href']
+            except:
+                continue
o = urlparse.urlparse(link, 'http')
-            if o.netloc and 'google' not in o.netloc:
+            if o.netloc and ('google' not in o.netloc) and ('youtube' not in o.netloc):
yield link

# Prepare the URL for the next request

Changelog:
- add a youtube and google links filter
- ‘href’ bug fixed (see below)

Traceback (most recent call last):
File "./google-mario.py", line 158, in
for url in search(query, stop=20):
File "./google-mario.py", line 141, in search
link = a['href']
File "build/bdist.macosx-10.6-universal/egg/BeautifulSoup.py", line 541, in __getitem__
KeyError: 'href'

All credits to Mario Vilas who wrote this quite simple python Google Search, thanks Mario.

You can download new and fixed Google Search, here.