1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-16 17:40:11 +01:00
searxng/searx/engines/startpage.py

80 lines
2.1 KiB
Python
Raw Normal View History

## Startpage (Web)
#
# @website https://startpage.com
# @provide-api no (nothing found)
#
# @using-api no
# @results HTML
# @stable no (HTML can change)
# @parse url, title, content
#
# @todo paging
2013-11-09 18:39:20 +01:00
from urllib import urlencode
2013-10-19 18:29:39 +02:00
from lxml import html
2014-08-06 14:43:44 +02:00
from cgi import escape
import re
# engine dependent config
categories = ['general']
# there is a mechanism to block "bot" search (probably the parameter qid), require storing of qid's between mulitble search-calls
#paging = False
language_support = True
2013-10-19 18:29:39 +02:00
# search-url
base_url = 'https://startpage.com/'
search_url = base_url + 'do/search'
2013-10-19 18:29:39 +02:00
# specific xpath variables
# ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
# not ads: div[@class="result"] are the direct childs of div[@id="results"]
results_xpath = '//div[@class="result"]'
link_xpath = './/h3/a'
2014-01-30 02:10:32 +01:00
2014-01-24 09:35:27 +01:00
# do search-request
2013-10-19 18:29:39 +02:00
def request(query, params):
offset = (params['pageno'] - 1) * 10
2013-11-09 18:39:20 +01:00
query = urlencode({'q': query})[2:]
2013-10-19 18:29:39 +02:00
params['url'] = search_url
params['method'] = 'POST'
2014-01-30 02:10:32 +01:00
params['data'] = {'query': query,
'startat': offset}
# set language if specified
if params['language'] != 'all':
params['data']['with_language'] = 'lang_' + params['language'].split('_')[0]
2013-10-19 18:29:39 +02:00
return params
# get response from search-request
2013-10-19 18:29:39 +02:00
def response(resp):
results = []
2013-10-24 21:00:44 +02:00
dom = html.fromstring(resp.content)
# parse results
for result in dom.xpath(results_xpath):
link = result.xpath(link_xpath)[0]
2013-10-24 23:43:39 +02:00
url = link.attrib.get('href')
2014-08-06 14:43:44 +02:00
title = escape(link.text_content())
# block google-ad url's
if re.match("^http(s|)://www.google.[a-z]+/aclk.*$", url):
continue
2014-02-11 13:13:51 +01:00
if result.xpath('./p[@class="desc"]'):
2014-08-06 14:43:44 +02:00
content = escape(result.xpath('./p[@class="desc"]')[0].text_content())
else:
content = ''
2014-01-24 09:35:27 +01:00
# append result
results.append({'url': url,
'title': title,
'content': content})
2014-01-24 09:35:27 +01:00
# return results
2013-10-19 18:29:39 +02:00
return results