logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

dictzone.py (1645B)


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