2014-12-01 12:26:38 +01:00
|
|
|
## Stackoverflow (It)
|
2014-12-13 13:56:02 +01:00
|
|
|
#
|
2014-12-01 12:26:38 +01:00
|
|
|
# @website https://stackoverflow.com/
|
|
|
|
# @provide-api not clear (https://api.stackexchange.com/docs/advanced-search)
|
2014-12-13 13:56:02 +01:00
|
|
|
#
|
2014-12-01 12:26:38 +01:00
|
|
|
# @using-api no
|
|
|
|
# @results HTML
|
|
|
|
# @stable no (HTML can change)
|
|
|
|
# @parse url, title, content
|
|
|
|
|
2014-05-10 11:56:19 +02:00
|
|
|
from urlparse import urljoin
|
|
|
|
from cgi import escape
|
|
|
|
from urllib import urlencode
|
|
|
|
from lxml import html
|
|
|
|
|
2014-12-01 12:26:38 +01:00
|
|
|
# engine dependent config
|
2014-05-10 11:56:19 +02:00
|
|
|
categories = ['it']
|
2014-12-01 12:26:38 +01:00
|
|
|
paging = True
|
2014-05-10 11:56:19 +02:00
|
|
|
|
2014-12-01 12:26:38 +01:00
|
|
|
# search-url
|
2014-05-10 11:56:19 +02:00
|
|
|
url = 'http://stackoverflow.com/'
|
|
|
|
search_url = url+'search?{query}&page={pageno}'
|
|
|
|
|
2014-12-01 12:26:38 +01:00
|
|
|
# specific xpath variables
|
|
|
|
results_xpath = '//div[contains(@class,"question-summary")]'
|
|
|
|
link_xpath = './/div[@class="result-link"]//a|.//div[@class="summary"]//h3//a'
|
|
|
|
title_xpath = './/text()'
|
|
|
|
content_xpath = './/div[@class="excerpt"]//text()'
|
2014-05-10 11:56:19 +02:00
|
|
|
|
|
|
|
|
2014-12-01 12:26:38 +01:00
|
|
|
# do search-request
|
2014-05-10 11:56:19 +02:00
|
|
|
def request(query, params):
|
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
|
|
|
pageno=params['pageno'])
|
2014-12-01 12:26:38 +01:00
|
|
|
|
2014-05-10 11:56:19 +02:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-12-01 12:26:38 +01:00
|
|
|
# get response from search-request
|
2014-05-10 11:56:19 +02:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-12-01 12:26:38 +01:00
|
|
|
|
2014-05-10 11:56:19 +02:00
|
|
|
dom = html.fromstring(resp.text)
|
2014-12-01 12:26:38 +01:00
|
|
|
|
|
|
|
# parse results
|
|
|
|
for result in dom.xpath(results_xpath):
|
|
|
|
link = result.xpath(link_xpath)[0]
|
2014-05-10 11:56:19 +02:00
|
|
|
href = urljoin(url, link.attrib.get('href'))
|
2014-12-01 12:26:38 +01:00
|
|
|
title = escape(' '.join(link.xpath(title_xpath)))
|
|
|
|
content = escape(' '.join(result.xpath(content_xpath)))
|
|
|
|
|
|
|
|
# append result
|
2014-12-13 13:56:02 +01:00
|
|
|
results.append({'url': href,
|
|
|
|
'title': title,
|
2014-12-01 12:26:38 +01:00
|
|
|
'content': content})
|
|
|
|
|
|
|
|
# return results
|
2014-05-10 11:56:19 +02:00
|
|
|
return results
|