【发布时间】:2020-05-05 18:07:54
【问题描述】:
我无法通过 Nokogiri 解析获得链接列表,https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/
我做错了什么?
links = Nokoiri::HTML('https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/')
或
links = Nokoiri::XML('https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/')
--->
#(Document:0x3fcdda1b988c {
name = "document",
children = [
#(DTD:0x3fcdda1b5b24 { name = "html" }),
#(Element:0x3fcdda1b46fc {
name = "html",
children = [
#(Element:0x3fcdda1b0804 {
name = "body",
children = [
#(Element:0x3fcdda1ac920 {
name = "p",
children = [ #(Text "https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/")]
})]
})]
})]
})
puts links.to_html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/</p></body></html>
=> nil
【问题讨论】:
-
在寻求帮助调试时,我们需要最少的代码和输入数据来证明问题和您所需的输出。超出最小限度的任何事情都会浪费我们的时间来帮助您,这会浪费您的时间。请参阅“MCVE”和“How to debug small programs”。要求我们从网站加载页面意味着我们必须搜索它以找到您正在谈论的部分。在这方面浪费了我们太多时间,我们会继续前进,所以请帮助我们为您提供帮助。
-
使用
nokogiri或wget或curl检索浏览器看到的确切页面。这些工具不处理 DHTML,因此它们不支持 CSS 或 JavaScript,它们返回的页面是 Nokogiri 处理的基础。 -
@the Tin Man 这个问题有什么不清楚的地方?一个简单的问题——为什么 Nokogiri 不能解析这个页面的名称 简单的答案——如果需要 js,Nokogiri 不会解析页面这就是我需要弄清楚问题所在。我已经找到了一些不依赖于 chromedriver 并且能够使用 ruby 下载更新版本而无需 curl 的选项。如果我可以自动化一个脚本来检查系统中驱动程序的当前版本,并用站点上的最新版本替换它,为什么我需要 curl?我不明白你的不满......
-
在编写任何代码之前,您应该使用其中一种工具来查看页面以确定它在做什么,或者至少关闭浏览器中的 JavaScript 并查看页面元素的作用不出现。任何不可见的东西很可能需要 JavaScript 处理。如果您已将其作为第一步,那么您甚至不需要问这个问题。 SO 在这方面有很多问题,所以搜索和阅读会给你指点。
-
另外,当询问像这样的网络抓取问题时,我们需要最少的代码和输入数据来测试问题在问题本身中。如果您这样做了,您将在编写和测试代码时自己回答您的问题。
标签: ruby xml-parsing html-parsing nokogiri