264def response(resp):
265 '''Scrap *results* from the response (see :ref:`engine results`).'''
266 if no_result_for_http_status and resp.status_code in no_result_for_http_status:
267 return []
268
269 raise_for_httperror(resp)
270
271 results = []
272
273 if not resp.text:
274 return results
275
276 dom = html.fromstring(resp.text)
277 is_onion = 'onions' in categories
278
279 if results_xpath:
280 for result in eval_xpath_list(dom, results_xpath):
281
282 url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)
283 title = extract_text(eval_xpath_list(result, title_xpath, min_len=1))
284 content = extract_text(eval_xpath_list(result, content_xpath))
285 tmp_result = {'url': url, 'title': title, 'content': content}
286
287
288 if thumbnail_xpath:
289 thumbnail_xpath_result = eval_xpath_list(result, thumbnail_xpath)
290 if len(thumbnail_xpath_result) > 0:
291 tmp_result['thumbnail'] = extract_url(thumbnail_xpath_result, search_url)
292
293
294 if cached_xpath:
295 tmp_result['cached_url'] = cached_url + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
296
297 if is_onion:
298 tmp_result['is_onion'] = True
299
300 results.append(tmp_result)
301
302 else:
303 if cached_xpath:
304 for url, title, content, cached in zip(
305 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
306 map(extract_text, eval_xpath_list(dom, title_xpath)),
307 map(extract_text, eval_xpath_list(dom, content_xpath)),
308 map(extract_text, eval_xpath_list(dom, cached_xpath)),
309 ):
310 results.append(
311 {
312 'url': url,
313 'title': title,
314 'content': content,
315 'cached_url': cached_url + cached,
316 'is_onion': is_onion,
317 }
318 )
319 else:
320 for url, title, content in zip(
321 (extract_url(x, search_url) for x in eval_xpath_list(dom, url_xpath)),
322 map(extract_text, eval_xpath_list(dom, title_xpath)),
323 map(extract_text, eval_xpath_list(dom, content_xpath)),
324 ):
325 results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
326
327 if suggestion_xpath:
328 for suggestion in eval_xpath(dom, suggestion_xpath):
329 results.append({'suggestion': extract_text(suggestion)})
330
331 logger.debug("found %s results", len(results))
332 return results