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