2013-10-11 17:28:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
import re
|
2014-09-30 16:15:32 +00:00
|
|
|
|
|
|
|
from six.moves import urllib
|
2013-10-11 17:28:32 +00:00
|
|
|
import ox
|
|
|
|
from ox import strip_tags, decode_html
|
|
|
|
from ox.cache import read_url
|
|
|
|
|
|
|
|
|
|
|
|
def find(query, timeout=ox.cache.cache_timeout):
|
2014-09-30 16:15:32 +00:00
|
|
|
if not isinstance(query, bytes):
|
2013-10-11 17:28:32 +00:00
|
|
|
query = query.encode('utf-8')
|
2014-09-30 16:15:32 +00:00
|
|
|
params = urllib.parse.urlencode({'q': query})
|
2013-10-11 17:28:32 +00:00
|
|
|
url = 'http://duckduckgo.com/html/?' + params
|
|
|
|
data = read_url(url, timeout=timeout).decode('utf-8')
|
|
|
|
results = []
|
|
|
|
regex = '<a .*?class="large" href="(.+?)">(.*?)</a>.*?<div class="snippet">(.*?)</div>'
|
|
|
|
for r in re.compile(regex, re.DOTALL).findall(data):
|
|
|
|
results.append((strip_tags(decode_html(r[1])), r[0], strip_tags(decode_html(r[2]))))
|
|
|
|
return results
|
|
|
|
|