У меня есть HTML-код и вы хотите получить контент под элементом <body>
. Однако, что бы я ни пытался, после того, как HTML анализируется с помощью Nokogiri, все внутри <doctype>
<head>
и также становятся частью <body>
элемента и когда я извлечь <body>
элемент, я вижу вещи внутри <doctype>
и <meta>
и <script>
тегов тоже.Анализ синтаксического кода HTML с помощью Nokogiri
Мой оригинальный HTML является:
<!DOCTYPE html \"about:legacy-compat\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<title>Some Title</title>
<meta name='viewport' id='helloviewport' content='initial-scale=1.0,maximum-scale=2.5' />
<link rel='stylesheet' id='hello-stylesheet' type='text/css' href='some-4ac294cd125e1a062562aca1c83714ff.css'/>
<script id='hello-javascript' type='text/javascript' src='/hello/hello.js'></script>
</head>
<body marginwidth=\"6\" marginheight=\"6\" leftmargin=\"6\" topmargin=\"6\">
<div class=\"hello-status\">Hello World</div>
<div valign=\"top\"></div>
</body>
</html>
Решение я использую:
parsed_html = Nokogiri::HTML(my_html)
body_tag_content = parsed_html.at('body')
puts body_tag_content.inner_html
Что я получаю:
<p>about:legacy-compat\"></p>
\n
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
\n
<title>Some title</title>
\n
<meta name='viewport' id='helloviewport' content='initial-scale=1.0,maximum-scale=2.5' />
\n
<link rel='stylesheet' id='hello-stylesheet' type='text/css' href='some-4ac294cd125e1a062562aca1c83714ff.css'/>
\n<script id='hello-javascript' type='text/javascript' src='/hello/hello.js'></script>
<div class=\"hello-status\">Hello World</div>
\n
<div valign=\"top\">\n\n</div>
Что я ожидал:
<div class=\"hello-status\">Hello World</div>
\n
<div valign=\"top\">\n\n</div>
Любая идея, что здесь происходит?
Да я понял, «О: Наследие-Compat» был вопрос, но не был уверен, почему HTML разбора не обрабатывал его. Удаление его перед разбором работ. –