destatis.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """DeStatis
  3. """
  4. from urllib.parse import urlencode
  5. from lxml import html
  6. from searx.utils import eval_xpath, eval_xpath_list, extract_text
  7. about = {
  8. 'website': 'https://www.destatis.de',
  9. 'official_api_documentation': 'https://destatis.api.bund.dev/',
  10. 'use_official_api': False,
  11. 'require_api_key': False,
  12. 'results': 'HTML',
  13. 'language': 'de',
  14. }
  15. categories = []
  16. paging = True
  17. base_url = "https://www.destatis.de"
  18. search_url = f"{base_url}/SiteGlobals/Forms/Suche/Expertensuche_Formular.html"
  19. # pylint: disable-next=line-too-long
  20. results_xpath = '//div[contains(@class, "l-content-wrapper")]/div[contains(@class, "row")]/div[contains(@class, "column")]/div[contains(@class, "c-result"){extra}]'
  21. results_xpath_filter_recommended = " and not(contains(@class, 'c-result--recommended'))"
  22. url_xpath = './/a/@href'
  23. title_xpath = './/a/text()'
  24. date_xpath = './/a/span[contains(@class, "c-result__date")]'
  25. content_xpath = './/div[contains(@class, "column")]/p/text()'
  26. doctype_xpath = './/div[contains(@class, "c-result__doctype")]/p'
  27. def request(query, params):
  28. args = {
  29. 'templateQueryString': query,
  30. 'gtp': f"474_list%3D{params['pageno']}",
  31. }
  32. params['url'] = f"{search_url}?{urlencode(args)}"
  33. return params
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. # filter out suggested results on further page because they're the same on each page
  38. extra_xpath = results_xpath_filter_recommended if resp.search_params['pageno'] > 1 else ''
  39. res_xpath = results_xpath.format(extra=extra_xpath)
  40. for result in eval_xpath_list(dom, res_xpath):
  41. doctype = extract_text(eval_xpath(result, doctype_xpath))
  42. date = extract_text(eval_xpath(result, date_xpath))
  43. metadata = [meta for meta in (doctype, date) if meta != ""]
  44. results.append(
  45. {
  46. 'url': base_url + "/" + extract_text(eval_xpath(result, url_xpath)),
  47. 'title': extract_text(eval_xpath(result, title_xpath)),
  48. 'content': extract_text(eval_xpath(result, content_xpath)),
  49. 'metadata': ', '.join(metadata),
  50. }
  51. )
  52. return results