dictzone.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Dictzone
  4. """
  5. from urllib.parse import urljoin
  6. from lxml import html
  7. from searx.utils import eval_xpath
  8. # about
  9. about = {
  10. "website": 'https://dictzone.com/',
  11. "wikidata_id": None,
  12. "official_api_documentation": None,
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. engine_type = 'online_dictionary'
  18. categories = ['general']
  19. url = 'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  20. weight = 100
  21. results_xpath = './/table[@id="r"]/tr'
  22. https_support = True
  23. def request(query, params):
  24. params['url'] = url.format(from_lang=params['from_lang'][2], to_lang=params['to_lang'][2], query=params['query'])
  25. return params
  26. def response(resp):
  27. results = []
  28. dom = html.fromstring(resp.text)
  29. for k, result in enumerate(eval_xpath(dom, results_xpath)[1:]):
  30. try:
  31. from_result, to_results_raw = eval_xpath(result, './td')
  32. except:
  33. continue
  34. to_results = []
  35. for to_result in eval_xpath(to_results_raw, './p/a'):
  36. t = to_result.text_content()
  37. if t.strip():
  38. to_results.append(to_result.text_content())
  39. results.append(
  40. {
  41. 'url': urljoin(str(resp.url), '?%d' % k),
  42. 'title': from_result.text_content(),
  43. 'content': '; '.join(to_results),
  44. }
  45. )
  46. return results