searxng/searx/engines/stackoverflow.py

26 lines
800 B
Python
Raw Normal View History

2013-10-16 22:27:25 +00:00
from lxml import html
from urlparse import urljoin
2013-10-17 18:43:05 +00:00
from cgi import escape
2013-10-20 19:31:16 +00:00
from urllib import urlencode
2013-10-16 22:27:25 +00:00
2013-10-17 19:07:09 +00:00
categories = ['it']
2013-10-23 21:55:37 +00:00
url = 'http://stackoverflow.com/'
search_url = url+'search?'
2013-10-16 22:27:25 +00:00
def request(query, params):
2013-10-20 19:31:16 +00:00
params['url'] = search_url + urlencode({'q': query})
2013-10-16 22:27:25 +00:00
return params
def response(resp):
results = []
dom = html.fromstring(resp.text)
for result in dom.xpath('//div[@class="question-summary search-result"]'):
link = result.xpath('.//div[@class="result-link"]//a')[0]
2013-10-23 21:55:37 +00:00
href = urljoin(url, link.attrib.get('href'))
2014-01-11 11:39:39 +00:00
title = escape(' '.join(link.xpath('.//text()')))
2013-10-17 18:43:05 +00:00
content = escape(' '.join(result.xpath('.//div[@class="excerpt"]//text()')))
2013-10-23 21:55:37 +00:00
results.append({'url': href, 'title': title, 'content': content})
2013-10-16 22:27:25 +00:00
return results