Ponysearch/searx/engines/stackoverflow.py

28 lines
847 B
Python
Raw Normal View History

2013-10-17 00:27:25 +02:00
from lxml import html
from urlparse import urljoin
2013-10-17 20:43:05 +02:00
from cgi import escape
2013-10-20 21:31:16 +02:00
from urllib import urlencode
2013-10-17 00:27:25 +02:00
2013-10-17 21:07:09 +02:00
categories = ['it']
2013-10-17 00:27:25 +02:00
base_url = 'http://stackoverflow.com/'
2013-10-20 21:31:16 +02:00
search_url = base_url+'search?'
2013-10-17 00:27:25 +02:00
def request(query, params):
global search_url
2013-10-20 21:31:16 +02:00
params['url'] = search_url + urlencode({'q': query})
2013-10-17 00:27:25 +02:00
return params
def response(resp):
global base_url
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]
url = urljoin(base_url, link.attrib.get('href'))
title = ' '.join(link.xpath('.//text()'))
2013-10-17 20:43:05 +02:00
content = escape(' '.join(result.xpath('.//div[@class="excerpt"]//text()')))
2013-10-17 00:27:25 +02:00
results.append({'url': url, 'title': title, 'content': content})
return results