searxng/searx/engines/xpath.py

106 lines
3.1 KiB
Python
Raw Normal View History

2013-10-26 00:22:20 +00:00
from lxml import html
from urllib import urlencode, unquote
2013-10-26 00:22:20 +00:00
from urlparse import urlparse, urljoin
from lxml.etree import _ElementStringResult
2014-01-23 10:08:08 +00:00
from searx.utils import html_to_text
2013-10-26 00:22:20 +00:00
2014-01-20 01:31:20 +00:00
search_url = None
url_xpath = None
2013-10-26 00:22:20 +00:00
content_xpath = None
2014-01-20 01:31:20 +00:00
title_xpath = None
suggestion_xpath = ''
results_xpath = ''
2013-10-26 00:22:20 +00:00
2014-01-20 01:31:20 +00:00
'''
if xpath_results is list, extract the text from each result and concat the list
2014-01-20 01:31:20 +00:00
if xpath_results is a xml element, extract all the text node from it
( text_content() method from lxml )
if xpath_results is a string element, then it's already done
'''
2014-01-20 01:31:20 +00:00
def extract_text(xpath_results):
2013-10-26 00:22:20 +00:00
if type(xpath_results) == list:
# it's list of result : concat everything using recursive call
2013-10-26 00:22:20 +00:00
if not len(xpath_results):
raise Exception('Empty url resultset')
result = ''
for e in xpath_results:
result = result + extract_text(e)
return result
elif type(xpath_results) == _ElementStringResult:
# it's a string
return ''.join(xpath_results)
2013-10-26 00:22:20 +00:00
else:
# it's a element
2014-01-23 10:08:08 +00:00
return html_to_text(xpath_results.text_content())
def extract_url(xpath_results):
url = extract_text(xpath_results)
if url.startswith('//'):
# add http or https to this kind of url //example.com/
parsed_search_url = urlparse(search_url)
url = parsed_search_url.scheme+url
elif url.startswith('/'):
# fix relative url to the search engine
url = urljoin(search_url, url)
# normalize url
url = normalize_url(url)
return url
def normalize_url(url):
2013-10-26 00:22:20 +00:00
parsed_url = urlparse(url)
# add a / at this end of the url if there is no path
2013-10-26 00:22:20 +00:00
if not parsed_url.netloc:
raise Exception('Cannot parse url')
2013-10-27 11:01:03 +00:00
if not parsed_url.path:
url += '/'
# FIXME : hack for yahoo
2014-01-20 01:31:20 +00:00
if parsed_url.hostname == 'search.yahoo.com'\
and parsed_url.path.startswith('/r'):
p = parsed_url.path
mark = p.find('/**')
if mark != -1:
return unquote(p[mark+3:]).decode('utf-8')
2013-10-26 00:22:20 +00:00
return url
2013-10-26 00:22:20 +00:00
def request(query, params):
query = urlencode({'q': query})[2:]
params['url'] = search_url.format(query=query)
params['query'] = query
return params
def response(resp):
results = []
dom = html.fromstring(resp.text)
if results_xpath:
for result in dom.xpath(results_xpath):
url = extract_url(result.xpath(url_xpath))
2014-01-20 01:31:20 +00:00
title = extract_text(result.xpath(title_xpath)[0])
content = extract_text(result.xpath(content_xpath)[0])
results.append({'url': url, 'title': title, 'content': content})
else:
2014-01-12 17:48:38 +00:00
for url, title, content in zip(
2014-01-20 01:31:20 +00:00
map(extract_url, dom.xpath(url_xpath)),
map(extract_text, dom.xpath(title_xpath)),
map(extract_text, dom.xpath(content_xpath))
):
results.append({'url': url, 'title': title, 'content': content})
if not suggestion_xpath:
return results
for suggestion in dom.xpath(suggestion_xpath):
results.append({'suggestion': extract_text(suggestion)})
2013-10-26 00:22:20 +00:00
return results