yahoo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. offset = (params['pageno'] - 1) * 10 + 1
  64. language = _get_language(params)
  65. params['url'] = _get_url(query, offset, language, params['time_range'])
  66. # TODO required?
  67. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  68. .format(lang=language)
  69. return params
  70. # get response from search-request
  71. def response(resp):
  72. results = []
  73. dom = html.fromstring(resp.text)
  74. try:
  75. results_num = int(dom.xpath('//div[@class="compPagination"]/span[last()]/text()')[0]
  76. .split()[0].replace(',', ''))
  77. results.append({'number_of_results': results_num})
  78. except:
  79. pass
  80. # parse results
  81. for result in dom.xpath(results_xpath):
  82. try:
  83. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  84. title = extract_text(result.xpath(title_xpath)[0])
  85. except:
  86. continue
  87. content = extract_text(result.xpath(content_xpath)[0])
  88. # append result
  89. results.append({'url': url,
  90. 'title': title,
  91. 'content': content})
  92. # if no suggestion found, return results
  93. suggestions = dom.xpath(suggestion_xpath)
  94. if not suggestions:
  95. return results
  96. # parse suggestion
  97. for suggestion in suggestions:
  98. # append suggestion
  99. results.append({'suggestion': extract_text(suggestion)})
  100. # return results
  101. return results