2013-10-16 22:27:25 +00:00
|
|
|
from urllib import quote
|
|
|
|
from lxml import html
|
|
|
|
from urlparse import urljoin
|
2013-10-17 18:43:05 +00:00
|
|
|
from cgi import escape
|
2013-10-16 22:27:25 +00:00
|
|
|
|
|
|
|
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()'))
|
2013-10-17 18:43:05 +00:00
|
|
|
content = escape(' '.join(result.xpath('.//div[@class="excerpt"]//text()')))
|
2013-10-16 22:27:25 +00:00
|
|
|
results.append({'url': url, 'title': title, 'content': content})
|
|
|
|
return results
|