213 '''Build request parameters (see :ref:`engine request`).'''
215 if params[
'language'] !=
'all':
216 lang = params[
'language'][:2]
219 if params.get(
'time_range'):
220 time_range_val = time_range_map.get(params.get(
'time_range'))
221 time_range = time_range_url.format(time_range_val=time_range_val)
224 if params[
'safesearch']:
225 safe_search = safe_search_map[params[
'safesearch']]
228 'query': urlencode({
'q': query})[2:],
230 'pageno': (params[
'pageno'] - 1) * page_size + first_page_num,
231 'time_range': time_range,
232 'safe_search': safe_search,
235 params[
'cookies'].update(cookies)
236 params[
'headers'].update(headers)
238 params[
'url'] = search_url.format(**fargs)
239 params[
'soft_max_redirects'] = soft_max_redirects
241 params[
'raise_for_httperror'] =
False
247 '''Scrap *results* from the response (see :ref:`engine results`).'''
248 if no_result_for_http_status
and resp.status_code
in no_result_for_http_status:
251 raise_for_httperror(resp)
254 dom = html.fromstring(resp.text)
255 is_onion =
'onions' in categories
258 for result
in eval_xpath_list(dom, results_xpath):
260 url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)
261 title = extract_text(eval_xpath_list(result, title_xpath, min_len=1))
262 content = extract_text(eval_xpath_list(result, content_xpath))
263 tmp_result = {
'url': url,
'title': title,
'content': content}
267 thumbnail_xpath_result = eval_xpath_list(result, thumbnail_xpath)
268 if len(thumbnail_xpath_result) > 0:
269 tmp_result[
'thumbnail'] = extract_url(thumbnail_xpath_result, search_url)
273 tmp_result[
'cached_url'] = cached_url + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
276 tmp_result[
'is_onion'] =
True
278 results.append(tmp_result)
282 for url, title, content, cached
in zip(
283 (extract_url(x, search_url)
for x
in eval_xpath_list(dom, url_xpath)),
284 map(extract_text, eval_xpath_list(dom, title_xpath)),
285 map(extract_text, eval_xpath_list(dom, content_xpath)),
286 map(extract_text, eval_xpath_list(dom, cached_xpath)),
293 'cached_url': cached_url + cached,
294 'is_onion': is_onion,
298 for url, title, content
in zip(
299 (extract_url(x, search_url)
for x
in eval_xpath_list(dom, url_xpath)),
300 map(extract_text, eval_xpath_list(dom, title_xpath)),
301 map(extract_text, eval_xpath_list(dom, content_xpath)),
303 results.append({
'url': url,
'title': title,
'content': content,
'is_onion': is_onion})
306 for suggestion
in eval_xpath(dom, suggestion_xpath):
307 results.append({
'suggestion': extract_text(suggestion)})
309 logger.debug(
"found %s results", len(results))