Ponysearch/searx/engines/startpage.py

39 lines
1.1 KiB
Python
Raw Normal View History

2013-11-09 18:39:20 +01:00
from urllib import urlencode
2013-10-19 18:29:39 +02:00
from lxml import html
base_url = None
search_url = None
2013-10-19 18:29:39 +02:00
2014-01-30 02:10:32 +01:00
# TODO paging
paging = False
2014-01-24 09:35:27 +01:00
2013-10-19 18:29:39 +02:00
def request(query, params):
global search_url
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': (params['pageno'] - 1) * 10} # offset
2013-10-19 18:29:39 +02:00
return params
def response(resp):
global base_url
results = []
2013-10-24 21:00:44 +02:00
dom = html.fromstring(resp.content)
# ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
2014-01-20 02:31:20 +01:00
# not ads: div[@class="result"] are the direct childs of div[@id="results"]
for result in dom.xpath('//div[@id="results"]/div[@class="result"]'):
2013-10-19 18:29:39 +02:00
link = result.xpath('.//h3/a')[0]
2013-10-24 23:43:39 +02:00
url = link.attrib.get('href')
title = link.text_content()
2014-01-24 09:35:27 +01:00
content = ''
if len(result.xpath('./p[@class="desc"]')):
content = result.xpath('./p[@class="desc"]')[0].text_content()
2014-01-24 09:35:27 +01:00
2013-10-19 18:29:39 +02:00
results.append({'url': url, 'title': title, 'content': content})
2014-01-24 09:35:27 +01:00
2013-10-19 18:29:39 +02:00
return results