.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
duden.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Duden
3
4"""
5
6import re
7from urllib.parse import quote, urljoin
8from lxml import html
9from searx.utils import extract_text, eval_xpath, eval_xpath_list, eval_xpath_getindex
10from searx.network import raise_for_httperror
11
12# about
13about = {
14 "website": 'https://www.duden.de',
15 "wikidata_id": 'Q73624591',
16 "official_api_documentation": None,
17 "use_official_api": False,
18 "require_api_key": False,
19 "results": 'HTML',
20 "language": 'de',
21}
22
23categories = ['dictionaries']
24paging = True
25
26# search-url
27base_url = 'https://www.duden.de/'
28search_url = base_url + 'suchen/dudenonline/{query}?search_api_fulltext=&page={offset}'
29
30
31def request(query, params):
32
33 offset = params['pageno'] - 1
34 if offset == 0:
35 search_url_fmt = base_url + 'suchen/dudenonline/{query}'
36 params['url'] = search_url_fmt.format(query=quote(query))
37 else:
38 params['url'] = search_url.format(offset=offset, query=quote(query))
39 # after the last page of results, spelling corrections are returned after a HTTP redirect
40 # whatever the page number is
41 params['soft_max_redirects'] = 1
42 params['raise_for_httperror'] = False
43 return params
44
45
46def response(resp):
47 results = []
48
49 if resp.status_code == 404:
50 return results
51
52 raise_for_httperror(resp)
53
54 dom = html.fromstring(resp.text)
55
56 number_of_results_element = eval_xpath_getindex(
57 dom, '//a[@class="active" and contains(@href,"/suchen/dudenonline")]/span/text()', 0, default=None
58 )
59 if number_of_results_element is not None:
60 number_of_results_string = re.sub('[^0-9]', '', number_of_results_element)
61 results.append({'number_of_results': int(number_of_results_string)})
62
63 for result in eval_xpath_list(dom, '//section[not(contains(@class, "essay"))]'):
64 url = eval_xpath_getindex(result, './/h2/a', 0).get('href')
65 url = urljoin(base_url, url)
66 title = eval_xpath(result, 'string(.//h2/a)').strip()
67 content = extract_text(eval_xpath(result, './/p'))
68 # append result
69 results.append({'url': url, 'title': title, 'content': content})
70
71 return results
request(query, params)
Definition duden.py:31