yahoo_news.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. from urllib import urlencode
  3. from lxml import html
  4. from searx.engines.xpath import extract_text, extract_url
  5. from searx.engines.yahoo import parse_url
  6. from datetime import datetime
  7. categories = ['news']
  8. search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
  9. results_xpath = '//div[@class="res"]'
  10. url_xpath = './/h3/a/@href'
  11. title_xpath = './/h3/a'
  12. content_xpath = './/div[@class="abstr"]'
  13. publishedDate_xpath = './/span[@class="timestamp"]'
  14. suggestion_xpath = '//div[@id="satat"]//a'
  15. paging = True
  16. def request(query, params):
  17. offset = (params['pageno'] - 1) * 10 + 1
  18. if params['language'] == 'all':
  19. language = 'en'
  20. else:
  21. language = params['language'].split('_')[0]
  22. params['url'] = search_url.format(offset=offset,
  23. query=urlencode({'p': query}))
  24. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  25. .format(lang=language)
  26. return params
  27. def response(resp):
  28. results = []
  29. dom = html.fromstring(resp.text)
  30. for result in dom.xpath(results_xpath):
  31. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  32. title = extract_text(result.xpath(title_xpath)[0])
  33. content = extract_text(result.xpath(content_xpath)[0])
  34. # Feb 20 04:02am
  35. publishedDate = datetime.strptime(extract_text(result.xpath(publishedDate_xpath)[0]),"%b %d %H:%M%p")
  36. #publishedDate.replace(year=2014)
  37. results.append({'url': url, 'title': title, 'content': content,'publishedDate':publishedDate})
  38. if not suggestion_xpath:
  39. return results
  40. for suggestion in dom.xpath(suggestion_xpath):
  41. results.append({'suggestion': extract_text(suggestion)})
  42. return results