duckduckgo_definitions.py 962 B

1234567891011121314151617181920212223242526272829303132
  1. import json
  2. from searx import base_result_template
  3. def request(query, params):
  4. params['url'] = 'http://api.duckduckgo.com/?q=%s&format=json&pretty=0' % query
  5. return params
  6. def response(resp):
  7. search_res = json.loads(resp.text)
  8. results = []
  9. if 'Definition' in search_res:
  10. res = {'title' : search_res.get('Heading', '')
  11. ,'content' : search_res.get('Definition', '')
  12. ,'url' : search_res.get('AbstractURL', '')
  13. }
  14. results.append(base_result_template.format(**res))
  15. return results
  16. #from lxml import html
  17. #def request(query, params):
  18. # params['method'] = 'POST'
  19. # params['url'] = 'https://duckduckgo.com/html'
  20. # params['data']['q'] = query
  21. # return params
  22. #
  23. #
  24. #def response(resp):
  25. # dom = html.fromstring(resp.text)
  26. # results = dom.xpath('//div[@class="results_links results_links_deep web-result"]')
  27. # return [html.tostring(x) for x in results]