yahoo.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. time_range_support = True
  20. # search-url
  21. base_url = 'https://search.yahoo.com/'
  22. search_url = 'search?{query}&b={offset}&fl=1&vl=lang_{lang}'
  23. search_url_with_time = 'search?{query}&b={offset}&fl=1&vl=lang_{lang}&age={age}&btf={btf}&fr2=time'
  24. # specific xpath variables
  25. results_xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' Sr ')]"
  26. url_xpath = './/h3/a/@href'
  27. title_xpath = './/h3/a'
  28. content_xpath = './/div[@class="compText aAbs"]'
  29. suggestion_xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' AlsoTry ')]//a"
  30. time_range_dict = {'day': ['1d', 'd'],
  31. 'week': ['1w', 'w'],
  32. 'month': ['1m', 'm']}
  33. # remove yahoo-specific tracking-url
  34. def parse_url(url_string):
  35. endings = ['/RS', '/RK']
  36. endpositions = []
  37. start = url_string.find('http', url_string.find('/RU=') + 1)
  38. for ending in endings:
  39. endpos = url_string.rfind(ending)
  40. if endpos > -1:
  41. endpositions.append(endpos)
  42. if start == 0 or len(endpositions) == 0:
  43. return url_string
  44. else:
  45. end = min(endpositions)
  46. return unquote(url_string[start:end])
  47. def _get_url(query, offset, language, time_range):
  48. if time_range in time_range_dict:
  49. return base_url + search_url_with_time.format(offset=offset,
  50. query=urlencode({'p': query}),
  51. lang=language,
  52. age=time_range_dict[time_range][0],
  53. btf=time_range_dict[time_range][1])
  54. return base_url + search_url.format(offset=offset,
  55. query=urlencode({'p': query}),
  56. lang=language)
  57. def _get_language(params):
  58. if params['language'] == 'all':
  59. return 'en'
  60. return params['language'].split('_')[0]
  61. # do search-request
  62. def request(query, params):
  63. if params['time_range'] and params['time_range'] not in time_range_dict:
  64. return params
  65. offset = (params['pageno'] - 1) * 10 + 1
  66. language = _get_language(params)
  67. params['url'] = _get_url(query, offset, language, params['time_range'])
  68. # TODO required?
  69. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  70. .format(lang=language)
  71. return params
  72. # get response from search-request
  73. def response(resp):
  74. results = []
  75. dom = html.fromstring(resp.text)
  76. try:
  77. results_num = int(dom.xpath('//div[@class="compPagination"]/span[last()]/text()')[0]
  78. .split()[0].replace(',', ''))
  79. results.append({'number_of_results': results_num})
  80. except:
  81. pass
  82. # parse results
  83. for result in dom.xpath(results_xpath):
  84. try:
  85. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  86. title = extract_text(result.xpath(title_xpath)[0])
  87. except:
  88. continue
  89. content = extract_text(result.xpath(content_xpath)[0])
  90. # append result
  91. results.append({'url': url,
  92. 'title': title,
  93. 'content': content})
  94. # if no suggestion found, return results
  95. suggestions = dom.xpath(suggestion_xpath)
  96. if not suggestions:
  97. return results
  98. # parse suggestion
  99. for suggestion in suggestions:
  100. # append suggestion
  101. results.append({'suggestion': extract_text(suggestion)})
  102. # return results
  103. return results