searxngRebrandZaclys/searx/engines/yandex.py

74 lines
1.9 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
2015-10-31 14:27:23 +00:00
"""
Yahoo (Web)
"""
from urllib.parse import urlencode, urlparse
2015-10-31 14:27:23 +00:00
from lxml import html
2016-11-30 17:43:03 +00:00
from searx import logger
from searx.exceptions import SearxEngineCaptchaException
2015-10-31 14:27:23 +00:00
logger = logger.getChild('yandex engine')
# about
about = {
"website": 'https://yandex.ru/',
"wikidata_id": 'Q5281',
"official_api_documentation": "?",
"use_official_api": False,
"require_api_key": False,
"results": 'HTML',
}
2015-10-31 14:27:23 +00:00
# engine dependent config
categories = ['general']
paging = True
language_support = True # TODO
default_tld = 'com'
language_map = {'ru': 'ru',
2016-10-30 02:04:01 +00:00
'ua': 'ua',
'be': 'by',
'kk': 'kz',
'tr': 'com.tr'}
2015-10-31 14:27:23 +00:00
# search-url
base_url = 'https://yandex.{tld}/'
2015-10-31 14:27:23 +00:00
search_url = 'search/?{query}&p={page}'
2016-12-11 03:57:42 +00:00
results_xpath = '//li[@class="serp-item"]'
2015-10-31 14:27:23 +00:00
url_xpath = './/h2/a/@href'
title_xpath = './/h2/a//text()'
2016-12-11 03:57:42 +00:00
content_xpath = './/div[@class="text-container typo typo_text_m typo_line_m organic__text"]//text()'
2015-10-31 14:27:23 +00:00
def request(query, params):
lang = params['language'].split('-')[0]
host = base_url.format(tld=language_map.get(lang) or default_tld)
2016-01-18 11:47:31 +00:00
params['url'] = host + search_url.format(page=params['pageno'] - 1,
query=urlencode({'text': query}))
2015-10-31 14:27:23 +00:00
return params
# get response from search-request
def response(resp):
resp_url = urlparse(resp.url)
if resp_url.path.startswith('/showcaptcha'):
raise SearxEngineCaptchaException()
2015-10-31 14:27:23 +00:00
dom = html.fromstring(resp.text)
results = []
for result in dom.xpath(results_xpath):
try:
res = {'url': result.xpath(url_xpath)[0],
'title': ''.join(result.xpath(title_xpath)),
'content': ''.join(result.xpath(content_xpath))}
2015-10-31 14:27:23 +00:00
except:
logger.exception('yandex parse crash')
continue
results.append(res)
return results