searxngRebrandZaclys/searx/engines/startpage.py

131 lines
3.7 KiB
Python
Raw Normal View History

2014-11-17 09:19:23 +00:00
# Startpage (Web)
#
# @website https://startpage.com
# @provide-api no (nothing found)
2014-11-17 09:19:23 +00:00
#
# @using-api no
# @results HTML
# @stable no (HTML can change)
# @parse url, title, content
#
# @todo paging
2013-10-19 16:29:39 +00:00
from lxml import html
from dateutil import parser
from datetime import datetime, timedelta
import re
2015-02-06 16:31:10 +00:00
from searx.engines.xpath import extract_text
from searx.languages import language_codes
# engine dependent config
categories = ['general']
2014-11-17 09:19:23 +00:00
# there is a mechanism to block "bot" search
# (probably the parameter qid), require
# storing of qid's between mulitble search-calls
paging = True
language_support = True
2013-10-19 16:29:39 +00:00
# search-url
base_url = 'https://startpage.com/'
search_url = base_url + 'do/search'
2013-10-19 16:29:39 +00: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="w-gl__result"]'
link_xpath = './/a[@class="w-gl__result-title"]'
content_xpath = './/p[@class="w-gl__description"]'
2014-01-30 01:10:32 +00:00
2014-01-24 08:35:27 +00:00
# do search-request
2013-10-19 16:29:39 +00:00
def request(query, params):
2013-10-19 16:29:39 +00:00
params['url'] = search_url
params['method'] = 'POST'
params['data'] = {
'query': query,
'page': params['pageno'],
'cat': 'web',
'cmd': 'process_search',
'engine0': 'v1all',
}
# set language if specified
if params['language'] != 'all':
language = 'english'
for lc, _, _, lang in language_codes:
if lc == params['language']:
language = lang
params['data']['language'] = language
params['data']['lui'] = language
2013-10-19 16:29:39 +00:00
return params
# get response from search-request
2013-10-19 16:29:39 +00:00
def response(resp):
results = []
2016-11-30 17:43:03 +00:00
dom = html.fromstring(resp.text)
2014-11-17 09:19:23 +00:00
# parse results
for result in dom.xpath(results_xpath):
2014-11-17 09:19:23 +00:00
links = result.xpath(link_xpath)
if not links:
continue
link = links[0]
2013-10-24 21:43:39 +00:00
url = link.attrib.get('href')
# block google-ad url's
2016-07-11 13:29:47 +00:00
if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
continue
# block startpage search url's
2016-07-11 13:29:47 +00:00
if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
continue
title = extract_text(link)
2015-02-06 16:31:10 +00:00
2018-12-14 20:08:04 +00:00
if result.xpath(content_xpath):
content = extract_text(result.xpath(content_xpath))
else:
content = ''
2014-01-24 08:35:27 +00:00
published_date = None
# check if search result starts with something like: "2 Sep 2014 ... "
2016-07-11 13:29:47 +00:00
if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
2016-01-18 11:47:31 +00:00
date_pos = content.find('...') + 4
date_string = content[0:date_pos - 5]
published_date = parser.parse(date_string, dayfirst=True)
# fix content string
content = content[date_pos:]
# check if search result starts with something like: "5 days ago ... "
2016-07-11 13:29:47 +00:00
elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
2016-01-18 11:47:31 +00:00
date_pos = content.find('...') + 4
date_string = content[0:date_pos - 5]
# calculate datetime
published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
# fix content string
content = content[date_pos:]
if published_date:
# append result
results.append({'url': url,
'title': title,
'content': content,
'publishedDate': published_date})
else:
# append result
results.append({'url': url,
'title': title,
'content': content})
2014-01-24 08:35:27 +00:00
# return results
2013-10-19 16:29:39 +00:00
return results