summaryrefslogtreecommitdiff
path: root/searx/engines/stackoverflow.py
blob: 24afe9eac89d55841449817b5e0199ed50f407cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from urllib import quote
from lxml import html
from urlparse import urljoin
from cgi import escape

base_url = 'http://stackoverflow.com/'
search_url = base_url+'search?q='

def request(query, params):
    global search_url
    query = quote(query.replace(' ', '+'), safe='+')
    params['url'] = search_url + query
    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()'))
        content = escape(' '.join(result.xpath('.//div[@class="excerpt"]//text()')))
        results.append({'url': url, 'title': title, 'content': content})
    return results