1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/stackoverflow.py

59 lines
1.5 KiB
Python
Raw Normal View History

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
2015-02-09 13:30:16 +01:00
from searx.engines.xpath import extract_text
2014-05-10 11:56:19 +02:00
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'
2015-02-09 13:30:16 +01:00
content_xpath = './/div[@class="excerpt"]'
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'))
2015-02-09 13:30:16 +01:00
title = escape(extract_text(link))
content = escape(extract_text(result.xpath(content_xpath)))
2014-12-01 12:26:38 +01:00
# 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