246def response(resp):
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:
249 return []
250
251 raise_for_httperror(resp)
252
253 results = []
254 dom = html.fromstring(resp.text)
255 is_onion = 'onions' in categories
256
257 if results_xpath:
258 for result in eval_xpath_list(dom, results_xpath):
259
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}
264
265
266 if thumbnail_xpath:
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)
270
271
272 if cached_xpath:
273 tmp_result['cached_url'] = cached_url + extract_text(eval_xpath_list(result, cached_xpath, min_len=1))
274
275 if is_onion:
276 tmp_result['is_onion'] = True
277
278 results.append(tmp_result)
279
280 else:
281 if cached_xpath:
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)),
287 ):
288 results.append(
289 {
290 'url': url,
291 'title': title,
292 'content': content,
293 'cached_url': cached_url + cached,
294 'is_onion': is_onion,
295 }
296 )
297 else:
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)),
302 ):
303 results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
304
305 if suggestion_xpath:
306 for suggestion in eval_xpath(dom, suggestion_xpath):
307 results.append({'suggestion': extract_text(suggestion)})
308
309 logger.debug("found %s results", len(results))
310 return results