dictzone.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Dictzone
  3. @website https://dictzone.com/
  4. @provide-api no
  5. @using-api no
  6. @results HTML (using search portal)
  7. @stable no (HTML can change)
  8. @parse url, title, content
  9. """
  10. from urllib.parse import urljoin
  11. from lxml import html
  12. from searx.utils import eval_xpath
  13. engine_type = 'online_dictionnary'
  14. categories = ['general']
  15. url = 'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  16. weight = 100
  17. results_xpath = './/table[@id="r"]/tr'
  18. https_support = True
  19. def request(query, params):
  20. params['url'] = url.format(from_lang=params['from_lang'][2],
  21. to_lang=params['to_lang'][2],
  22. query=params['query'])
  23. return params
  24. def response(resp):
  25. results = []
  26. dom = html.fromstring(resp.text)
  27. for k, result in enumerate(eval_xpath(dom, results_xpath)[1:]):
  28. try:
  29. from_result, to_results_raw = eval_xpath(result, './td')
  30. except:
  31. continue
  32. to_results = []
  33. for to_result in eval_xpath(to_results_raw, './p/a'):
  34. t = to_result.text_content()
  35. if t.strip():
  36. to_results.append(to_result.text_content())
  37. results.append({
  38. 'url': urljoin(resp.url, '?%d' % k),
  39. 'title': from_result.text_content(),
  40. 'content': '; '.join(to_results)
  41. })
  42. return results