.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.xpath Namespace Reference

Functions

 request (query, params)
 
 response (resp)
 

Variables

 search_url = None
 
str lang_all = 'en'
 
list no_result_for_http_status = []
 
int soft_max_redirects = 0
 
str results_xpath = ''
 
 url_xpath = None
 
 content_xpath = None
 
 title_xpath = None
 
bool thumbnail_xpath = False
 
str suggestion_xpath = ''
 
str cached_xpath = ''
 
str cached_url = ''
 
dict cookies = {}
 
dict headers = {}
 
bool paging = False
 
int page_size = 1
 
int first_page_num = 1
 
bool time_range_support = False
 
str time_range_url = '&hours={time_range_val}'
 
dict time_range_map
 
bool safe_search_support = False
 
dict safe_search_map = {0: '&filter=none', 1: '&filter=moderate', 2: '&filter=strict'}
 

Detailed Description

The XPath engine is a *generic* engine with which it is possible to configure
engines in the settings.

.. _XPath selector: https://quickref.me/xpath.html#xpath-selectors

Configuration
=============

Request:

- :py:obj:`search_url`
- :py:obj:`lang_all`
- :py:obj:`soft_max_redirects`
- :py:obj:`cookies`
- :py:obj:`headers`

Paging:

- :py:obj:`paging`
- :py:obj:`page_size`
- :py:obj:`first_page_num`

Time Range:

- :py:obj:`time_range_support`
- :py:obj:`time_range_url`
- :py:obj:`time_range_map`

Safe-Search:

- :py:obj:`safe_search_support`
- :py:obj:`safe_search_map`

Response:

- :py:obj:`no_result_for_http_status`

`XPath selector`_:

- :py:obj:`results_xpath`
- :py:obj:`url_xpath`
- :py:obj:`title_xpath`
- :py:obj:`content_xpath`
- :py:obj:`thumbnail_xpath`
- :py:obj:`suggestion_xpath`


Example
=======

Here is a simple example of a XPath engine configured in the :ref:`settings
engine` section, further read :ref:`engines-dev`.

.. code:: yaml

  - name : bitbucket
    engine : xpath
    paging : True
    search_url : https://bitbucket.org/repo/all/{pageno}?name={query}
    url_xpath : //article[@class="repo-summary"]//a[@class="repo-link"]/@href
    title_xpath : //article[@class="repo-summary"]//a[@class="repo-link"]
    content_xpath : //article[@class="repo-summary"]/p

Implementations
===============

Function Documentation

◆ request()

searx.engines.xpath.request ( query,
params )
Build request parameters (see :ref:`engine request`).

Definition at line 212 of file xpath.py.

212def request(query, params):
213 '''Build request parameters (see :ref:`engine request`).'''
214 lang = lang_all
215 if params['language'] != 'all':
216 lang = params['language'][:2]
217
218 time_range = ''
219 if params.get('time_range'):
220 time_range_val = time_range_map.get(params.get('time_range'))
221 time_range = time_range_url.format(time_range_val=time_range_val)
222
223 safe_search = ''
224 if params['safesearch']:
225 safe_search = safe_search_map[params['safesearch']]
226
227 fargs = {
228 'query': urlencode({'q': query})[2:],
229 'lang': lang,
230 'pageno': (params['pageno'] - 1) * page_size + first_page_num,
231 'time_range': time_range,
232 'safe_search': safe_search,
233 }
234
235 params['cookies'].update(cookies)
236 params['headers'].update(headers)
237
238 params['url'] = search_url.format(**fargs)
239 params['soft_max_redirects'] = soft_max_redirects
240
241 params['raise_for_httperror'] = False
242
243 return params
244
245

◆ response()

searx.engines.xpath.response ( resp)
Scrap *results* from the response (see :ref:`engine results`).

Definition at line 246 of file xpath.py.

246def response(resp): # pylint: disable=too-many-branches
247 '''Scrap *results* from the response (see :ref:`engine results`).'''
248 if no_result_for_http_status and resp.status_code in no_result_for_http_status:
249 return []
250
251 raise_for_httperror(resp)
252
253 results = []
254 dom = html.fromstring(resp.text)
255 is_onion = 'onions' in categories
256
257 if results_xpath:
258 for result in eval_xpath_list(dom, results_xpath):
259
260 url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)
261 title = extract_text(eval_xpath_list(result, title_xpath, min_len=1))
262 content = extract_text(eval_xpath_list(result, content_xpath))
263 tmp_result = {'url': url, 'title': title, 'content': content}
264
265 # add thumbnail if available
266 if thumbnail_xpath:
267 thumbnail_xpath_result = eval_xpath_list(result, thumbnail_xpath)
268 if len(thumbnail_xpath_result) > 0:
269 tmp_result['thumbnail'] = extract_url(thumbnail_xpath_result, search_url)
270
271 # add alternative cached url if available
272 if cached_xpath:
273 tmp_result['cached_url'] = cached_url + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
274
275 if is_onion:
276 tmp_result['is_onion'] = True
277
278 results.append(tmp_result)
279
280 else:
281 if cached_xpath:
282 for url, title, content, cached in zip(
283 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
284 map(extract_text, eval_xpath_list(dom, title_xpath)),
285 map(extract_text, eval_xpath_list(dom, content_xpath)),
286 map(extract_text, eval_xpath_list(dom, cached_xpath)),
287 ):
288 results.append(
289 {
290 'url': url,
291 'title': title,
292 'content': content,
293 'cached_url': cached_url + cached,
294 'is_onion': is_onion,
295 }
296 )
297 else:
298 for url, title, content in zip(
299 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
300 map(extract_text, eval_xpath_list(dom, title_xpath)),
301 map(extract_text, eval_xpath_list(dom, content_xpath)),
302 ):
303 results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
304
305 if suggestion_xpath:
306 for suggestion in eval_xpath(dom, suggestion_xpath):
307 results.append({'suggestion': extract_text(suggestion)})
308
309 logger.debug("found %s results", len(results))
310 return results

Variable Documentation

◆ cached_url

str searx.engines.xpath.cached_url = ''

Definition at line 144 of file xpath.py.

◆ cached_xpath

str searx.engines.xpath.cached_xpath = ''

Definition at line 143 of file xpath.py.

◆ content_xpath

searx.engines.xpath.content_xpath = None

Definition at line 131 of file xpath.py.

◆ cookies

dict searx.engines.xpath.cookies = {}

Definition at line 146 of file xpath.py.

◆ first_page_num

int searx.engines.xpath.first_page_num = 1

Definition at line 161 of file xpath.py.

◆ headers

dict searx.engines.xpath.headers = {}

Definition at line 150 of file xpath.py.

◆ lang_all

str searx.engines.xpath.lang_all = 'en'

Definition at line 109 of file xpath.py.

◆ no_result_for_http_status

list searx.engines.xpath.no_result_for_http_status = []

Definition at line 114 of file xpath.py.

◆ page_size

int searx.engines.xpath.page_size = 1

Definition at line 157 of file xpath.py.

◆ paging

bool searx.engines.xpath.paging = False

Definition at line 154 of file xpath.py.

◆ results_xpath

str searx.engines.xpath.results_xpath = ''

Definition at line 125 of file xpath.py.

◆ safe_search_map

dict searx.engines.xpath.safe_search_map = {0: '&filter=none', 1: '&filter=moderate', 2: '&filter=strict'}

Definition at line 198 of file xpath.py.

◆ safe_search_support

bool searx.engines.xpath.safe_search_support = False

Definition at line 195 of file xpath.py.

◆ search_url

searx.engines.xpath.search_url = None

Definition at line 76 of file xpath.py.

◆ soft_max_redirects

int searx.engines.xpath.soft_max_redirects = 0

Definition at line 122 of file xpath.py.

◆ suggestion_xpath

str searx.engines.xpath.suggestion_xpath = ''

Definition at line 140 of file xpath.py.

◆ thumbnail_xpath

bool searx.engines.xpath.thumbnail_xpath = False

Definition at line 137 of file xpath.py.

◆ time_range_map

dict searx.engines.xpath.time_range_map
Initial value:
1= {
2 'day': 24,
3 'week': 24 * 7,
4 'month': 24 * 30,
5 'year': 24 * 365,
6}

Definition at line 177 of file xpath.py.

◆ time_range_support

bool searx.engines.xpath.time_range_support = False

Definition at line 164 of file xpath.py.

◆ time_range_url

str searx.engines.xpath.time_range_url = '&hours={time_range_val}'

Definition at line 167 of file xpath.py.

◆ title_xpath

searx.engines.xpath.title_xpath = None

Definition at line 134 of file xpath.py.

◆ url_xpath

searx.engines.xpath.url_xpath = None

Definition at line 128 of file xpath.py.