logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

stackoverflow.py (1458B)


  1. """
  2. Stackoverflow (It)
  3. @website https://stackoverflow.com/
  4. @provide-api not clear (https://api.stackexchange.com/docs/advanced-search)
  5. @using-api no
  6. @results HTML
  7. @stable no (HTML can change)
  8. @parse url, title, content
  9. """
  10. from lxml import html
  11. from searx.engines.xpath import extract_text
  12. from searx.url_utils import urlencode, urljoin
  13. # engine dependent config
  14. categories = ['it']
  15. paging = True
  16. # search-url
  17. url = 'https://stackoverflow.com/'
  18. search_url = url + 'search?{query}&page={pageno}'
  19. # specific xpath variables
  20. results_xpath = '//div[contains(@class,"question-summary")]'
  21. link_xpath = './/div[@class="result-link"]//a|.//div[@class="summary"]//h3//a'
  22. content_xpath = './/div[@class="excerpt"]'
  23. # do search-request
  24. def request(query, params):
  25. params['url'] = search_url.format(query=urlencode({'q': query}), pageno=params['pageno'])
  26. return params
  27. # get response from search-request
  28. def response(resp):
  29. results = []
  30. dom = html.fromstring(resp.text)
  31. # parse results
  32. for result in dom.xpath(results_xpath):
  33. link = result.xpath(link_xpath)[0]
  34. href = urljoin(url, link.attrib.get('href'))
  35. title = extract_text(link)
  36. content = extract_text(result.xpath(content_xpath))
  37. # append result
  38. results.append({'url': href,
  39. 'title': title,
  40. 'content': content})
  41. # return results
  42. return results