.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
geizhals.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Geizhals is a German website to compare the price of a product on the
3most common German shopping sites and find the lowest price.
4
5The sorting of the search results can be influenced by the following additions
6to the search term:
7
8``asc`` or ``price``
9 To sort by price in ascending order.
10
11``desc``
12 To sort by price in descending order.
13
14"""
15
16import re
17
18from urllib.parse import urlencode
19from lxml import html
20
21from searx.utils import eval_xpath, eval_xpath_list, extract_text
22
23about = {
24 'website': 'https://geizhals.de',
25 'wikidata_id': 'Q15977657',
26 'use_official_api': False,
27 'official_api_documentation': None,
28 'require_api_key': False,
29 'results': 'HTML',
30 'language': 'de',
31}
32paging = True
33categories = ['shopping']
34
35base_url = "https://geizhals.de"
36sort_order = 'relevance'
37
38SORT_RE = re.compile(r"sort:(\w+)")
39sort_order_map = {
40 'relevance': None,
41 'price': 'p',
42 'asc': 'p',
43 'desc': '-p',
44}
45
46
47def request(query, params):
48 sort = None
49
50 sort_order_path = SORT_RE.search(query)
51 if sort_order_path:
52 sort = sort_order_map.get(sort_order_path.group(1))
53 query = SORT_RE.sub("", query)
54 logger.debug(query)
55
56 args = {
57 'fs': query,
58 'pg': params['pageno'],
59 'toggle_all': 1, # load item specs
60 'sort': sort,
61 }
62 params['url'] = f"{base_url}/?{urlencode(args)}"
63
64 return params
65
66
67def response(resp):
68 results = []
69
70 dom = html.fromstring(resp.text)
71 for result in eval_xpath_list(dom, "//article[contains(@class, 'listview__item')]"):
72 content = []
73 for spec in eval_xpath_list(result, ".//div[contains(@class, 'specs-grid__item')]"):
74 content.append(f"{extract_text(eval_xpath(spec, './dt'))}: {extract_text(eval_xpath(spec, './dd'))}")
75
76 metadata = [
77 extract_text(eval_xpath(result, ".//div[contains(@class, 'stars-rating-label')]")),
78 extract_text(eval_xpath(result, ".//div[contains(@class, 'listview__offercount')]")),
79 ]
80
81 item = {
82 'template': 'products.html',
83 'url': (
84 base_url + "/" + extract_text(eval_xpath(result, ".//a[contains(@class, 'listview__name-link')]/@href"))
85 ),
86 'title': extract_text(eval_xpath(result, ".//h3[contains(@class, 'listview__name')]")),
87 'content': ' | '.join(content),
88 'thumbnail': extract_text(eval_xpath(result, ".//img[contains(@class, 'listview__image')]/@src")),
89 'metadata': ', '.join(item for item in metadata if item),
90 }
91
92 best_price = extract_text(eval_xpath(result, ".//a[contains(@class, 'listview__price-link')]")).split(" ")
93 if len(best_price) > 1:
94 item["price"] = f"Bestes Angebot: {best_price[1]}€"
95 results.append(item)
96
97 return results
request(query, params)
Definition geizhals.py:47