【发布时间】:2017-01-04 08:33:37
【问题描述】:
我正在处理一个在<html> 标签之外有一些text 的文档。当我在正文中读取数据时,它还会返回甚至不在 html 标记中的文本。
page_text = Nokogiri::HTML(open(file_path)).xpath("//body").text
p page_text
输出:
"WARC/1.0\nWARC-Type: response\nWARC-Date: 2012-02-11T04:48:01Z\nWARC-TREC-ID: clueweb12-0000tw-13-04988\nWARC-IP-Address: 184.85.26.15\nWARC-Payload-Digest: sha1:PNCB5NNAA766RLLISZ6ODV3FJZBCATKR\nWARC-Target-URI: http://www.allchocolate.com/health/basics/\nWARC-Record-ID: \nContent-Type: application/http; msgtype=response\nContent-Length: 14577\n\n\n\n\n sample document\n\n\n hello world\n\n"
文档:
WARC/1.0
WARC-Type: response
WARC-Date: 2012-02-11T04:48:01Z
WARC-TREC-ID: clueweb12-0000tw-13-04988
WARC-IP-Address: 184.85.26.15
WARC-Payload-Digest: sha1:PNCB5NNAA766RLLISZ6ODV3FJZBCATKR
WARC-Target-URI: http://www.allchocolate.com/health/basics/
WARC-Record-ID: <urn:uuid:ff32c863-5066-4f51-802a-f31d4af074d5>
Content-Type: application/http; msgtype=response
Content-Length: 14577
<!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>
<title>sample document</title>
</head>
<body>
hello world
</body>
</html>
【问题讨论】:
-
请阅读“minimal reproducible example”。不要使用图像向我们展示您的预期输出。链接腐烂然后中断,当你的问题发生时,你的问题就没有意义了。而是将信息复制/粘贴到您的问题中并正确格式化以提高可读性。在将其传递给 Nokogiri 之前,您需要从源中去除非 HTML。它不知道标题信息是什么,所以你只是混淆它。
-
@theTinMan 谢谢你的建议,我已经编辑了我的问题。
标签: html ruby parsing xpath nokogiri