yahoo_news.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Yahoo (News)
  2. #
  3. # @website https://news.yahoo.com
  4. # @provide-api yes (https://developer.yahoo.com/boss/search/)
  5. # $0.80/1000 queries
  6. #
  7. # @using-api no (because pricing)
  8. # @results HTML (using search portal)
  9. # @stable no (HTML can change)
  10. # @parse url, title, content, publishedDate
  11. from urllib import urlencode
  12. from lxml import html
  13. from searx.engines.xpath import extract_text, extract_url
  14. from searx.engines.yahoo import parse_url
  15. from datetime import datetime, timedelta
  16. import re
  17. from dateutil import parser
  18. # engine dependent config
  19. categories = ['news']
  20. paging = True
  21. language_support = True
  22. # search-url
  23. search_url = 'https://news.search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}' # noqa
  24. # specific xpath variables
  25. results_xpath = '//div[@class="res"]'
  26. url_xpath = './/h3/a/@href'
  27. title_xpath = './/h3/a'
  28. content_xpath = './/div[@class="abstr"]'
  29. publishedDate_xpath = './/span[@class="timestamp"]'
  30. suggestion_xpath = '//div[@id="satat"]//a'
  31. # do search-request
  32. def request(query, params):
  33. offset = (params['pageno'] - 1) * 10 + 1
  34. if params['language'] == 'all':
  35. language = 'en'
  36. else:
  37. language = params['language'].split('_')[0]
  38. params['url'] = search_url.format(offset=offset,
  39. query=urlencode({'p': query}),
  40. lang=language)
  41. # TODO required?
  42. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  43. .format(lang=language)
  44. return params
  45. # get response from search-request
  46. def response(resp):
  47. results = []
  48. dom = html.fromstring(resp.text)
  49. # parse results
  50. for result in dom.xpath(results_xpath):
  51. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  52. title = extract_text(result.xpath(title_xpath)[0])
  53. content = extract_text(result.xpath(content_xpath)[0])
  54. # parse publishedDate
  55. publishedDate = extract_text(result.xpath(publishedDate_xpath)[0])
  56. if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
  57. publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group())) # noqa
  58. else:
  59. if re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$",
  60. publishedDate):
  61. timeNumbers = re.findall(r'\d+', publishedDate)
  62. publishedDate = datetime.now()\
  63. - timedelta(hours=int(timeNumbers[0]))\
  64. - timedelta(minutes=int(timeNumbers[1]))
  65. else:
  66. publishedDate = parser.parse(publishedDate)
  67. if publishedDate.year == 1900:
  68. publishedDate = publishedDate.replace(year=datetime.now().year)
  69. # append result
  70. results.append({'url': url,
  71. 'title': title,
  72. 'content': content,
  73. 'publishedDate': publishedDate})
  74. # return results
  75. return results