303def _parse_search(resp) -> EngineResults:
304 result_list = EngineResults()
305
306 dom = html.fromstring(resp.text)
307
308
309
310 answer_tag = eval_xpath_getindex(dom, '//div[@class="answer"]', 0, default=None)
311 if answer_tag:
312 url = eval_xpath_getindex(dom, '//div[@id="featured_snippet"]/a[@class="result-header"]/@href', 0, default=None)
313 answer = extract_text(answer_tag)
314 if answer is not None:
315 result_list.add(result_list.types.Answer(answer=answer, url=url))
316
317
318 xpath_results = '//div[contains(@class, "snippet ")]'
319
320 for result in eval_xpath_list(dom, xpath_results):
321
322 url = eval_xpath_getindex(result, './/a[contains(@class, "h")]/@href', 0, default=None)
323 title_tag = eval_xpath_getindex(
324 result, './/a[contains(@class, "h")]//div[contains(@class, "title")]', 0, default=None
325 )
326 if url is None or title_tag is None or not urlparse(url).netloc:
327 continue
328
329 content: str = extract_text(
330 eval_xpath_getindex(result, './/div[contains(@class, "snippet-description")]', 0, default='')
331 )
332 pub_date_raw = eval_xpath(result, 'substring-before(.//div[contains(@class, "snippet-description")], "-")')
333 pub_date = _extract_published_date(pub_date_raw)
334 if pub_date and content.startswith(pub_date_raw):
335 content = content.lstrip(pub_date_raw).strip("- \n\t")
336
337 thumbnail = eval_xpath_getindex(result, './/img[contains(@class, "thumb")]/@src', 0, default='')
338
339 item = {
340 'url': url,
341 'title': extract_text(title_tag),
342 'content': content,
343 'publishedDate': pub_date,
344 'thumbnail': thumbnail,
345 }
346
347 video_tag = eval_xpath_getindex(
348 result, './/div[contains(@class, "video-snippet") and @data-macro="video"]', 0, default=None
349 )
350 if video_tag is not None:
351
352
353
354
355 iframe_src = get_embeded_stream_url(url)
356 if iframe_src:
357 item['iframe_src'] = iframe_src
358 item['template'] = 'videos.html'
359 item['thumbnail'] = eval_xpath_getindex(video_tag, './/img/@src', 0, default='')
360 pub_date_raw = extract_text(
361 eval_xpath(video_tag, './/div[contains(@class, "snippet-attributes")]/div/text()')
362 )
363 item['publishedDate'] = _extract_published_date(pub_date_raw)
364 else:
365 item['thumbnail'] = eval_xpath_getindex(video_tag, './/img/@src', 0, default='')
366
367 result_list.append(item)
368
369 return result_list
370
371