yahoo.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. Yahoo (Web)
  3. @website https://search.yahoo.com/web
  4. @provide-api yes (https://developer.yahoo.com/boss/search/),
  5. $0.80/1000 queries
  6. @using-api no (because pricing)
  7. @results HTML (using search portal)
  8. @stable no (HTML can change)
  9. @parse url, title, content, suggestion
  10. """
  11. from urllib import urlencode
  12. from urlparse import unquote
  13. from lxml import html
  14. from searx.engines.xpath import extract_text, extract_url
  15. # engine dependent config
  16. categories = ['general']
  17. paging = True
  18. language_support = True
  19. supported_languages = ["ar", "bg", "ca", "szh", "tzh", "hr", "cs", "da", "nl", "en",
  20. "et", "fi", "fr", "de", "el", "he", "hu", "is", "id", "it", "ja",
  21. "ko", "lv", "lt", "no", "fa", "pl", "pt", "ro", "ru", "sk", "sr",
  22. "sl", "es", "sv", "th", "tr"]
  23. time_range_support = True
  24. # search-url
  25. base_url = 'https://search.yahoo.com/'
  26. search_url = 'search?{query}&b={offset}&fl=1&vl=lang_{lang}'
  27. search_url_with_time = 'search?{query}&b={offset}&fl=1&vl=lang_{lang}&age={age}&btf={btf}&fr2=time'
  28. # specific xpath variables
  29. results_xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' Sr ')]"
  30. url_xpath = './/h3/a/@href'
  31. title_xpath = './/h3/a'
  32. content_xpath = './/div[@class="compText aAbs"]'
  33. suggestion_xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' AlsoTry ')]//a"
  34. time_range_dict = {'day': ['1d', 'd'],
  35. 'week': ['1w', 'w'],
  36. 'month': ['1m', 'm']}
  37. # remove yahoo-specific tracking-url
  38. def parse_url(url_string):
  39. endings = ['/RS', '/RK']
  40. endpositions = []
  41. start = url_string.find('http', url_string.find('/RU=') + 1)
  42. for ending in endings:
  43. endpos = url_string.rfind(ending)
  44. if endpos > -1:
  45. endpositions.append(endpos)
  46. if start == 0 or len(endpositions) == 0:
  47. return url_string
  48. else:
  49. end = min(endpositions)
  50. return unquote(url_string[start:end])
  51. def _get_url(query, offset, language, time_range):
  52. if time_range in time_range_dict:
  53. return base_url + search_url_with_time.format(offset=offset,
  54. query=urlencode({'p': query}),
  55. lang=language,
  56. age=time_range_dict[time_range][0],
  57. btf=time_range_dict[time_range][1])
  58. return base_url + search_url.format(offset=offset,
  59. query=urlencode({'p': query}),
  60. lang=language)
  61. def _get_language(params):
  62. if params['language'] == 'all':
  63. return 'en'
  64. elif params['language'][:2] == 'zh':
  65. if params['language'] == 'zh' or params['language'] == 'zh-CH':
  66. return 'szh'
  67. else:
  68. return 'tzh'
  69. else:
  70. return params['language'].split('-')[0]
  71. # do search-request
  72. def request(query, params):
  73. if params['time_range'] and params['time_range'] not in time_range_dict:
  74. return params
  75. offset = (params['pageno'] - 1) * 10 + 1
  76. language = _get_language(params)
  77. params['url'] = _get_url(query, offset, language, params['time_range'])
  78. # TODO required?
  79. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  80. .format(lang=language)
  81. return params
  82. # get response from search-request
  83. def response(resp):
  84. results = []
  85. dom = html.fromstring(resp.text)
  86. try:
  87. results_num = int(dom.xpath('//div[@class="compPagination"]/span[last()]/text()')[0]
  88. .split()[0].replace(',', ''))
  89. results.append({'number_of_results': results_num})
  90. except:
  91. pass
  92. # parse results
  93. for result in dom.xpath(results_xpath):
  94. try:
  95. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  96. title = extract_text(result.xpath(title_xpath)[0])
  97. except:
  98. continue
  99. content = extract_text(result.xpath(content_xpath)[0])
  100. # append result
  101. results.append({'url': url,
  102. 'title': title,
  103. 'content': content})
  104. # if no suggestion found, return results
  105. suggestions = dom.xpath(suggestion_xpath)
  106. if not suggestions:
  107. return results
  108. # parse suggestion
  109. for suggestion in suggestions:
  110. # append suggestion
  111. results.append({'suggestion': extract_text(suggestion)})
  112. # return results
  113. return results