python-ox/ox/web/duckduckgo.py
j 2de989e188 net/cache readUrl->read_url / Unicode -> unicode=True
format replace all CammelCase with under_score
2012-08-14 15:58:05 +02:00

22 lines
729 B
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
import urllib
import ox
from ox import strip_tags, decodeHtml
from ox.utils import json
from ox.cache import read_url
def find(query, timeout=ox.cache.cache_timeout):
if isinstance(query, unicode):
query = query.encode('utf-8')
params = urllib.urlencode({'q': query})
url = 'http://duckduckgo.com/html/?' + params
data = read_url(url, timeout=timeout, unicode=True)
results = []
regex = '<a .*?class="l le" href="(.+?)">(.*?)</a>.*?<div class="cra">(.*?)</div>'
for r in re.compile(regex, re.DOTALL).findall(data):
results.append((strip_tags(decodeHtml(r[1])), r[0], strip_tags(decodeHtml(r[2]))))
return results