yahoo.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. from urllib import urlencode
  3. from searx.engines.xpath import extract_text, extract_url
  4. from lxml import html
  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. params['url'] = search_url.format(offset=offset,
  16. query=urlencode({'p': query}))
  17. print params['url']
  18. return params
  19. def response(resp):
  20. results = []
  21. dom = html.fromstring(resp.text)
  22. for result in dom.xpath(results_xpath):
  23. url = extract_url(result.xpath(url_xpath), search_url)
  24. title = extract_text(result.xpath(title_xpath)[0])
  25. content = extract_text(result.xpath(content_xpath)[0])
  26. results.append({'url': url, 'title': title, 'content': content})
  27. if not suggestion_xpath:
  28. return results
  29. for suggestion in dom.xpath(suggestion_xpath):
  30. results.append({'suggestion': extract_text(suggestion)})
  31. return results