gpt4 book ai didi

html - 无法提取 html 表格行

转载 作者:数据小太阳 更新时间:2023-10-29 07:25:10 26 4
gpt4 key购买 nike

enter image description here

我尝试提取上表中列出的所有五行。

我使用 Ruby hpricot 库通过 xpath 表达式提取表行。

在我的示例中,我使用的 xpath 表达式是/html/body/center/table/tr。请注意,我已从表达式中删除了 tbody 标记,这通常是成功提取的情况。

奇怪的是,我得到的是结果中的前三行,但缺少最后两行。我只是不知道那里发生了什么。

编辑:代码没什么神奇的,只是根据要求附上它。

require 'open-uri'
require 'hpricot'

faculty = Hpricot(open("http://www.utm.utoronto.ca/7800.0.html"))
(faculty/"/html/body/center/table/tr").each do |text|
puts text.to_s
end

最佳答案

有问题的 HTML 文档无效。 (参见 http://validator.w3.org/check?uri=http%3A%2F%2Fwww.utm.utoronto.ca%2F7800.0.html。)Hpricot 以不同于您的浏览器的方式解析它——因此产生不同的结果——但这并不能真正归咎于它。在 HTML5 之前,没有关于如何解析无效 HTML 文档的标准。

我尝试用 Nokogiri 替换 Hpricot,它似乎给出了预期的解析。代码:

require 'open-uri'
require 'nokogiri'

faculty = Nokogiri.HTML(open("http://www.utm.utoronto.ca/7800.0.html"))

faculty.search("/html/body/center/table/tr").each do |text|
puts text
end

也许你应该换个地方?

关于html - 无法提取 html 表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204751/

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