1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-17 01:50:10 +01:00
searxng/searx/engines/duckduckgo.py

66 lines
1.8 KiB
Python
Raw Normal View History

2013-10-23 23:55:37 +02:00
from urllib import urlencode
2014-03-21 16:33:17 +01:00
from lxml.html import fromstring
2013-11-09 18:39:20 +01:00
from searx.utils import html_to_text
2013-10-14 23:09:13 +02:00
2014-03-21 16:33:17 +01:00
url = 'https://duckduckgo.com/html?{query}&s={offset}'
locale = 'us-en'
2013-10-14 23:09:13 +02:00
2014-03-29 16:38:45 +01:00
2013-10-14 23:09:13 +02:00
def request(query, params):
2014-01-30 01:03:19 +01:00
offset = (params['pageno'] - 1) * 30
2014-01-20 02:31:20 +01:00
q = urlencode({'q': query,
'l': locale})
2014-03-21 16:33:17 +01:00
params['url'] = url.format(query=q, offset=offset)
2013-10-14 23:09:13 +02:00
return params
def response(resp):
2014-03-29 16:38:45 +01:00
result_xpath = '//div[@class="results_links results_links_deep web-result"]' # noqa
2014-03-21 16:33:17 +01:00
url_xpath = './/a[@class="large"]/@href'
title_xpath = './/a[@class="large"]//text()'
content_xpath = './/div[@class="snippet"]//text()'
2013-10-15 19:11:43 +02:00
results = []
2014-03-21 16:33:17 +01:00
doc = fromstring(resp.text)
for r in doc.xpath(result_xpath):
2014-03-21 18:17:13 +01:00
try:
res_url = r.xpath(url_xpath)[-1]
except:
continue
2014-03-21 16:33:17 +01:00
if not res_url:
2013-10-15 19:11:43 +02:00
continue
2014-03-21 16:33:17 +01:00
title = html_to_text(''.join(r.xpath(title_xpath)))
content = html_to_text(''.join(r.xpath(content_xpath)))
results.append({'title': title,
'content': content,
'url': res_url})
2013-10-15 19:11:43 +02:00
return results
2014-03-21 16:33:17 +01:00
#from json import loads
#search_url = url + 'd.js?{query}&p=1&s={offset}'
#
#paging = True
#
#
#def request(query, params):
# offset = (params['pageno'] - 1) * 30
# q = urlencode({'q': query,
# 'l': locale})
# params['url'] = search_url.format(query=q, offset=offset)
# return params
#
#
#def response(resp):
# results = []
# search_res = loads(resp.text[resp.text.find('[{'):-2])[:-1]
# for r in search_res:
# if not r.get('t'):
# continue
# results.append({'title': r['t'],
# 'content': html_to_text(r['a']),
# 'url': r['u']})
# return results