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

Functions

 request (query, params)
 
EngineResults 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 225 of file xpath.py.

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

◆ response()

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

Definition at line 265 of file xpath.py.

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

Variable Documentation

◆ cached_url

str searx.engines.xpath.cached_url = ''

Definition at line 147 of file xpath.py.

◆ cached_xpath

str searx.engines.xpath.cached_xpath = ''

Definition at line 146 of file xpath.py.

◆ content_xpath

searx.engines.xpath.content_xpath = None

Definition at line 134 of file xpath.py.

◆ cookies

dict searx.engines.xpath.cookies = {}

Definition at line 149 of file xpath.py.

◆ first_page_num

int searx.engines.xpath.first_page_num = 1

Definition at line 174 of file xpath.py.

◆ headers

dict searx.engines.xpath.headers = {}

Definition at line 153 of file xpath.py.

◆ lang_all

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

Definition at line 112 of file xpath.py.

◆ method

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

Definition at line 157 of file xpath.py.

◆ no_result_for_http_status

list searx.engines.xpath.no_result_for_http_status = []

Definition at line 117 of file xpath.py.

◆ page_size

int searx.engines.xpath.page_size = 1

Definition at line 170 of file xpath.py.

◆ paging

bool searx.engines.xpath.paging = False

Definition at line 167 of file xpath.py.

◆ request_body

str searx.engines.xpath.request_body = ''

Definition at line 160 of file xpath.py.

◆ results_xpath

str searx.engines.xpath.results_xpath = ''

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

◆ safe_search_support

bool searx.engines.xpath.safe_search_support = False

Definition at line 208 of file xpath.py.

◆ search_url

searx.engines.xpath.search_url = None

Definition at line 79 of file xpath.py.

◆ soft_max_redirects

int searx.engines.xpath.soft_max_redirects = 0

Definition at line 125 of file xpath.py.

◆ suggestion_xpath

str searx.engines.xpath.suggestion_xpath = ''

Definition at line 143 of file xpath.py.

◆ thumbnail_xpath

bool searx.engines.xpath.thumbnail_xpath = False

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

◆ time_range_support

bool searx.engines.xpath.time_range_support = False

Definition at line 177 of file xpath.py.

◆ time_range_url

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

Definition at line 180 of file xpath.py.

◆ title_xpath

searx.engines.xpath.title_xpath = None

Definition at line 137 of file xpath.py.

◆ url_xpath

searx.engines.xpath.url_xpath = None

Definition at line 131 of file xpath.py.