使用 Nokogiri 问题解析 HTML
Issue parsing HTML using Nokogiri
我有一些HTML,希望获取<body>
元素下的内容。但是,无论我尝试什么,在使用 Nokogiri 解析 HTML 之后,<doctype>
和 <head>
中的所有内容也成为 <body>
元素的一部分,当我检索 <body>
元素,我在 <doctype>
以及 <meta>
和 <script>
标签中也看到了东西。
我原来的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>
知道这里发生了什么吗?
我通过首先清理原始 HTML 让你的示例工作。我从 Doctype 中删除了 "about:legacy-compat",这似乎把 Nokogiri 弄乱了:
# clean up the junk in the doctype
my_html.sub!("\"about:legacy-compat\"", "")
# parse and get the body
parsed_html = Nokogiri::HTML(my_html)
body_tag_content = parsed_html.at('body')
puts body_tag_content.inner_html
# => "\n <div class=\"hello-status\">Hello World</div>\n <div valign=\"top\"></div>\n "
一般来说,当您解析可能有问题的第三方数据(例如 HTML 时),您应该先清理它,这样解析器就不会阻塞并做出意想不到的事情。您可以通过 linter 或 "tidy" 工具 运行 HTML 来尝试自动清理它。当所有其他方法都失败时,您将不得不像上面那样手动清洁它。
HTML tidy/cleaning in Ruby 1.9
我有一些HTML,希望获取<body>
元素下的内容。但是,无论我尝试什么,在使用 Nokogiri 解析 HTML 之后,<doctype>
和 <head>
中的所有内容也成为 <body>
元素的一部分,当我检索 <body>
元素,我在 <doctype>
以及 <meta>
和 <script>
标签中也看到了东西。
我原来的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>
知道这里发生了什么吗?
我通过首先清理原始 HTML 让你的示例工作。我从 Doctype 中删除了 "about:legacy-compat",这似乎把 Nokogiri 弄乱了:
# clean up the junk in the doctype
my_html.sub!("\"about:legacy-compat\"", "")
# parse and get the body
parsed_html = Nokogiri::HTML(my_html)
body_tag_content = parsed_html.at('body')
puts body_tag_content.inner_html
# => "\n <div class=\"hello-status\">Hello World</div>\n <div valign=\"top\"></div>\n "
一般来说,当您解析可能有问题的第三方数据(例如 HTML 时),您应该先清理它,这样解析器就不会阻塞并做出意想不到的事情。您可以通过 linter 或 "tidy" 工具 运行 HTML 来尝试自动清理它。当所有其他方法都失败时,您将不得不像上面那样手动清洁它。
HTML tidy/cleaning in Ruby 1.9