searxngRebrandZaclys/searx/engines/startpage.py

48 lines
1.6 KiB
Python
Raw Normal View History

2013-11-09 17:39:20 +00:00
from urllib import urlencode
2013-10-19 16:29:39 +00:00
from lxml import html
2014-08-06 12:43:44 +00:00
from cgi import escape
2013-10-19 16:29:39 +00:00
base_url = None
search_url = None
2013-10-19 16:29:39 +00:00
2014-01-30 01:10:32 +00:00
# TODO paging
paging = False
2014-01-31 04:10:49 +00:00
# TODO complete list of country mapping
country_map = {'en_US': 'eng',
'en_UK': 'uk',
'nl_NL': 'ned'}
2014-01-30 01:10:32 +00:00
2014-01-24 08:35:27 +00:00
2013-10-19 16:29:39 +00:00
def request(query, params):
2013-11-09 17:39:20 +00:00
query = urlencode({'q': query})[2:]
2013-10-19 16:29:39 +00:00
params['url'] = search_url
params['method'] = 'POST'
2014-01-30 01:10:32 +00:00
params['data'] = {'query': query,
'startat': (params['pageno'] - 1) * 10} # offset
2014-01-31 04:10:49 +00:00
country = country_map.get(params['language'], 'eng')
params['cookies']['preferences'] = \
'lang_homepageEEEs/air/{country}/N1NsslEEE1N1Nfont_sizeEEEmediumN1Nrecent_results_filterEEE1N1Nlanguage_uiEEEenglishN1Ndisable_open_in_new_windowEEE0N1Ncolor_schemeEEEnewN1Nnum_of_resultsEEE10N1N'.format(country=country) # noqa
2013-10-19 16:29:39 +00:00
return params
def response(resp):
results = []
2013-10-24 19:00:44 +00:00
dom = html.fromstring(resp.content)
# ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
2014-01-20 01:31:20 +00:00
# not ads: div[@class="result"] are the direct childs of div[@id="results"]
2014-02-18 15:14:31 +00:00
for result in dom.xpath('//div[@class="result"]'):
2013-10-19 16:29:39 +00:00
link = result.xpath('.//h3/a')[0]
2013-10-24 21:43:39 +00:00
url = link.attrib.get('href')
2014-02-18 15:14:31 +00:00
if url.startswith('http://www.google.')\
or url.startswith('https://www.google.'):
continue
2014-08-06 12:43:44 +00:00
title = escape(link.text_content())
2014-01-24 08:35:27 +00:00
content = ''
2014-02-11 12:13:51 +00:00
if result.xpath('./p[@class="desc"]'):
2014-08-06 12:43:44 +00:00
content = escape(result.xpath('./p[@class="desc"]')[0].text_content())
2014-01-24 08:35:27 +00:00
2013-10-19 16:29:39 +00:00
results.append({'url': url, 'title': title, 'content': content})
2014-01-24 08:35:27 +00:00
2013-10-19 16:29:39 +00:00
return results