gpt4 book ai didi

ruby-on-rails - Nokogiri 无法在 CentOS 中读取/解析 HTML 文件的结构

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:52 25 4
gpt4 key购买 nike

我编写了一个脚本来解析上传到我们应用程序的 HTML 文件中的一些所需代码。在 OS X 上,这个过程工作正常。但是,当我上传到我们的测试服务器时,它没有。当我进入测试服务器上的控制台并尝试解析文件时,Nokogiri 将看不到结构 - 每次我得到一行输出而不是整个文档结构。我的脚本的其余部分没有被执行,因为 Nokogiri 没有遍历文档。寻求有关如何解决问题的帮助。

这是我用来打开文件并将其提供给 Nokogiri 的必要代码:

html = Nokogiri::HTML(File.open("index.html", "r"))

这是 html 等同于:

#<Nokogiri::HTML::Document:0x10d9bbf0 name="document" children=[#<Nokogiri::XML::DTD:0x10d9b81c name="html">]>

在 OS X 中,我得到了整个树,正如预期的那样。

这是 index.html 的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="zero.css" type="text/css" charset="utf-8" />
</head>
<body class="fullpage-vert" onunload="javascript:clearInterval(audioLoop);">
<div id="container">
<div id="danceHolder">
<img id="danceVid" src="1-1.jpg" width="320" height="480" alt="" />
</div>
<div id="introHolder">
<img id="introVid" src="0-1.jpg" width="320" height="480" alt="" />
<div id="ctabg"></div>
<div id="cta1"></div>
<div id="cta2"></div>
<div id="cta3"></div>
<div id="phone"></div>
<div id="logo"></div>
</div>
</div>
<a href="mmbridge:*">bridge test</a>
<frameset cols="25%,75%">
<frame src="frame_a.htm" />
<frame src="frame_b.htm" />
</frameset>
</body>
</html>

例如,当我尝试搜索框架集时,我一无所获:

html.css("frameset").size
0

我知道 Nokogiri 在 CentOS (2.6.2) 上安装的默认 Libxml2 版本有问题,但我已按照说明在新版本 (2.7.8) 上构建它。这是 nokogiri -v 的输出:

# Nokogiri (1.5.0)
---
warnings: []

nokogiri: 1.5.0
ruby:
version: 1.9.2
platform: x86_64-linux
description: ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
engine: ruby
libxml:
binding: extension
compiled: 2.7.8
loaded: 2.7.8

有没有其他人见过这样的行为?

最佳答案

出于某种原因,交换

html = Nokogiri::HTML(File.open("index.html", "r"))

对于

html = Nokogiri::HTML(File.read("index.html"))

有效,尽管现在它无法正确计算行号(所有内容都是行号 0)。

关于ruby-on-rails - Nokogiri 无法在 CentOS 中读取/解析 HTML 文件的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9167492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com