python-ox/ox/web/google.py

42 lines
1.2 KiB
Python
Raw Normal View History

2010-07-07 23:25:57 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
import urllib
import ox
2012-03-18 14:38:51 +00:00
from ox import stripTags, decodeHtml
2010-07-07 23:25:57 +00:00
DEFAULT_MAX_RESULTS = 10
DEFAULT_TIMEOUT = 24*60*60
def readUrlUnicode(url, data=None, headers=ox.net.DEFAULT_HEADERS, timeout=DEFAULT_TIMEOUT):
return ox.cache.readUrlUnicode(url, data, headers, timeout)
2010-07-07 23:25:57 +00:00
def quote_plus(s):
2010-07-28 13:04:43 +00:00
if not isinstance(s, str):
s = s.encode('utf-8')
return urllib.quote_plus(s)
2010-07-07 23:25:57 +00:00
def find(query, max_results=DEFAULT_MAX_RESULTS, timeout=DEFAULT_TIMEOUT):
2010-07-28 13:04:43 +00:00
"""
Return max_results tuples with title, url, description
>>> find("The Matrix site:imdb.com", 1)[0][0]
u'The Matrix (1999) - IMDb'
2010-07-28 13:04:43 +00:00
>>> find("The Matrix site:imdb.com", 1)[0][1]
u'http://www.imdb.com/title/tt0133093/'
2010-07-28 13:04:43 +00:00
"""
url = 'http://google.com/search?q=%s' % quote_plus(query)
data = readUrlUnicode(url, timeout=timeout)
2010-07-07 23:25:57 +00:00
results = []
2012-03-18 14:38:51 +00:00
data = re.sub('<span class="f">(.*?)</span>', '\\1', data)
for a in re.compile(
'<a href="(\S+?)" class=l .*?>(.*?)</a>.*?<span class="st">(.*?)<\/span>'
).findall(data):
results.append((stripTags(decodeHtml(a[1])), a[0], stripTags(decodeHtml(a[2]))))
2010-07-28 13:04:43 +00:00
if len(results) >= max_results:
break
2010-07-07 23:25:57 +00:00
return results