yahoo.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. categories = ['general']
  6. search_url = 'http://search.yahoo.com/search?{query}&b={offset}'
  7. results_xpath = '//div[@class="res"]'
  8. url_xpath = './/h3/a/@href'
  9. title_xpath = './/h3/a'
  10. content_xpath = './/div[@class="abstr"]'
  11. suggestion_xpath = '//div[@id="satat"]//a'
  12. paging = True
  13. def request(query, params):
  14. offset = (params['pageno'] - 1) * 10 + 1
  15. if params['language'] == 'all':
  16. language = 'en'
  17. else:
  18. language = params['language'].split('_')[0]
  19. params['url'] = search_url.format(offset=offset,
  20. query=urlencode({'p': query}))
  21. params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
  22. .format(lang=language)
  23. return params
  24. def response(resp):
  25. results = []
  26. dom = html.fromstring(resp.text)
  27. for result in dom.xpath(results_xpath):
  28. url = extract_url(result.xpath(url_xpath), search_url)
  29. title = extract_text(result.xpath(title_xpath)[0])
  30. content = extract_text(result.xpath(content_xpath)[0])
  31. results.append({'url': url, 'title': title, 'content': content})
  32. if not suggestion_xpath:
  33. return results
  34. for suggestion in dom.xpath(suggestion_xpath):
  35. results.append({'suggestion': extract_text(suggestion)})
  36. return results