dictzone.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. import re
  11. from urllib.parse import urljoin
  12. from lxml import html
  13. from searx.utils import is_valid_lang, eval_xpath
  14. categories = ['general']
  15. url = 'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  16. weight = 100
  17. parser_re = re.compile('.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
  18. results_xpath = './/table[@id="r"]/tr'
  19. https_support = True
  20. def request(query, params):
  21. m = parser_re.match(query)
  22. if not m:
  23. return params
  24. from_lang, to_lang, query = m.groups()
  25. from_lang = is_valid_lang(from_lang)
  26. to_lang = is_valid_lang(to_lang)
  27. if not from_lang or not to_lang:
  28. return params
  29. params['url'] = url.format(from_lang=from_lang[2],
  30. to_lang=to_lang[2],
  31. query=query)
  32. return params
  33. def response(resp):
  34. results = []
  35. dom = html.fromstring(resp.text)
  36. for k, result in enumerate(eval_xpath(dom, results_xpath)[1:]):
  37. try:
  38. from_result, to_results_raw = eval_xpath(result, './td')
  39. except:
  40. continue
  41. to_results = []
  42. for to_result in eval_xpath(to_results_raw, './p/a'):
  43. t = to_result.text_content()
  44. if t.strip():
  45. to_results.append(to_result.text_content())
  46. results.append({
  47. 'url': urljoin(resp.url, '?%d' % k),
  48. 'title': from_result.text_content(),
  49. 'content': '; '.join(to_results)
  50. })
  51. return results