logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

duden.py (2092B)


  1. """
  2. Duden
  3. @website https://www.duden.de
  4. @provide-api no
  5. @using-api no
  6. @results HTML (using search portal)
  7. @stable no (HTML can change)
  8. @parse url, title, content
  9. """
  10. from lxml import html, etree
  11. import re
  12. from searx.engines.xpath import extract_text
  13. from searx.url_utils import quote
  14. from searx import logger
  15. categories = ['general']
  16. paging = True
  17. language_support = False
  18. # search-url
  19. base_url = 'https://www.duden.de/'
  20. search_url = base_url + 'suchen/dudenonline/{query}?page={offset}'
  21. def request(query, params):
  22. '''pre-request callback
  23. params<dict>:
  24. method : POST/GET
  25. headers : {}
  26. data : {} # if method == POST
  27. url : ''
  28. category: 'search category'
  29. pageno : 1 # number of the requested page
  30. '''
  31. offset = (params['pageno'] - 1)
  32. params['url'] = search_url.format(offset=offset, query=quote(query))
  33. return params
  34. def response(resp):
  35. '''post-response callback
  36. resp: requests response object
  37. '''
  38. results = []
  39. dom = html.fromstring(resp.text)
  40. try:
  41. number_of_results_string = re.sub('[^0-9]', '', dom.xpath(
  42. '//a[@class="active" and contains(@href,"/suchen/dudenonline")]/span/text()')[0]
  43. )
  44. results.append({'number_of_results': int(number_of_results_string)})
  45. except:
  46. logger.debug("Couldn't read number of results.")
  47. pass
  48. for result in dom.xpath('//section[@class="wide" and not(contains(@style,"overflow:hidden"))]'):
  49. try:
  50. logger.debug("running for %s" % str(result))
  51. link = result.xpath('.//h2/a')[0]
  52. url = link.attrib.get('href')
  53. title = result.xpath('string(.//h2/a)')
  54. content = extract_text(result.xpath('.//p'))
  55. # append result
  56. results.append({'url': url,
  57. 'title': title,
  58. 'content': content})
  59. except:
  60. logger.debug('result parse error in:\n%s', etree.tostring(result, pretty_print=True))
  61. continue
  62. return results