.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 = {}
 
str method = 'GET'
 
str request_body = ''
 
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:`method`
- :py:obj:`request_body`
- :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 224 of file xpath.py.

224def request(query, params):
225 '''Build request parameters (see :ref:`engine request`).'''
226 lang = lang_all
227 if params['language'] != 'all':
228 lang = params['language'][:2]
229
230 time_range = ''
231 if params.get('time_range'):
232 time_range_val = time_range_map.get(params.get('time_range'))
233 time_range = time_range_url.format(time_range_val=time_range_val)
234
235 safe_search = ''
236 if params['safesearch']:
237 safe_search = safe_search_map[params['safesearch']]
238
239 fargs = {
240 'query': urlencode({'q': query})[2:],
241 'lang': lang,
242 'pageno': (params['pageno'] - 1) * page_size + first_page_num,
243 'time_range': time_range,
244 'safe_search': safe_search,
245 }
246
247 params['cookies'].update(cookies)
248 params['headers'].update(headers)
249
250 params['url'] = search_url.format(**fargs)
251 params['method'] = method
252
253 if request_body:
254 # don't url-encode the query if it's in the request body
255 fargs['query'] = query
256 params['data'] = request_body.format(**fargs)
257
258 params['soft_max_redirects'] = soft_max_redirects
259 params['raise_for_httperror'] = False
260
261 return params
262
263

◆ response()

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

Definition at line 264 of file xpath.py.

264def response(resp): # pylint: disable=too-many-branches
265 '''Scrap *results* from the response (see :ref:`engine results`).'''
266 if no_result_for_http_status and resp.status_code in no_result_for_http_status:
267 return []
268
269 raise_for_httperror(resp)
270
271 results = []
272
273 if not resp.text:
274 return results
275
276 dom = html.fromstring(resp.text)
277 is_onion = 'onions' in categories
278
279 if results_xpath:
280 for result in eval_xpath_list(dom, results_xpath):
281
282 url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)
283 title = extract_text(eval_xpath_list(result, title_xpath, min_len=1))
284 content = extract_text(eval_xpath_list(result, content_xpath))
285 tmp_result = {'url': url, 'title': title, 'content': content}
286
287 # add thumbnail if available
288 if thumbnail_xpath:
289 thumbnail_xpath_result = eval_xpath_list(result, thumbnail_xpath)
290 if len(thumbnail_xpath_result) > 0:
291 tmp_result['thumbnail'] = extract_url(thumbnail_xpath_result, search_url)
292
293 # add alternative cached url if available
294 if cached_xpath:
295 tmp_result['cached_url'] = cached_url + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
296
297 if is_onion:
298 tmp_result['is_onion'] = True
299
300 results.append(tmp_result)
301
302 else:
303 if cached_xpath:
304 for url, title, content, cached in zip(
305 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
306 map(extract_text, eval_xpath_list(dom, title_xpath)),
307 map(extract_text, eval_xpath_list(dom, content_xpath)),
308 map(extract_text, eval_xpath_list(dom, cached_xpath)),
309 ):
310 results.append(
311 {
312 'url': url,
313 'title': title,
314 'content': content,
315 'cached_url': cached_url + cached,
316 'is_onion': is_onion,
317 }
318 )
319 else:
320 for url, title, content in zip(
321 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
322 map(extract_text, eval_xpath_list(dom, title_xpath)),
323 map(extract_text, eval_xpath_list(dom, content_xpath)),
324 ):
325 results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
326
327 if suggestion_xpath:
328 for suggestion in eval_xpath(dom, suggestion_xpath):
329 results.append({'suggestion': extract_text(suggestion)})
330
331 logger.debug("found %s results", len(results))
332 return results

Variable Documentation

◆ cached_url

str searx.engines.xpath.cached_url = ''

Definition at line 146 of file xpath.py.

◆ cached_xpath

str searx.engines.xpath.cached_xpath = ''

Definition at line 145 of file xpath.py.

◆ content_xpath

searx.engines.xpath.content_xpath = None

Definition at line 133 of file xpath.py.

◆ cookies

dict searx.engines.xpath.cookies = {}

Definition at line 148 of file xpath.py.

◆ first_page_num

int searx.engines.xpath.first_page_num = 1

Definition at line 173 of file xpath.py.

◆ headers

dict searx.engines.xpath.headers = {}

Definition at line 152 of file xpath.py.

◆ lang_all

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

Definition at line 111 of file xpath.py.

◆ method

str searx.engines.xpath.method = 'GET'

Definition at line 156 of file xpath.py.

◆ no_result_for_http_status

list searx.engines.xpath.no_result_for_http_status = []

Definition at line 116 of file xpath.py.

◆ page_size

int searx.engines.xpath.page_size = 1

Definition at line 169 of file xpath.py.

◆ paging

bool searx.engines.xpath.paging = False

Definition at line 166 of file xpath.py.

◆ request_body

str searx.engines.xpath.request_body = ''

Definition at line 159 of file xpath.py.

◆ results_xpath

str searx.engines.xpath.results_xpath = ''

Definition at line 127 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 210 of file xpath.py.

◆ safe_search_support

bool searx.engines.xpath.safe_search_support = False

Definition at line 207 of file xpath.py.

◆ search_url

searx.engines.xpath.search_url = None

Definition at line 78 of file xpath.py.

◆ soft_max_redirects

int searx.engines.xpath.soft_max_redirects = 0

Definition at line 124 of file xpath.py.

◆ suggestion_xpath

str searx.engines.xpath.suggestion_xpath = ''

Definition at line 142 of file xpath.py.

◆ thumbnail_xpath

bool searx.engines.xpath.thumbnail_xpath = False

Definition at line 139 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 189 of file xpath.py.

◆ time_range_support

bool searx.engines.xpath.time_range_support = False

Definition at line 176 of file xpath.py.

◆ time_range_url

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

Definition at line 179 of file xpath.py.

◆ title_xpath

searx.engines.xpath.title_xpath = None

Definition at line 136 of file xpath.py.

◆ url_xpath

searx.engines.xpath.url_xpath = None

Definition at line 130 of file xpath.py.