新手问题 执行 Wombat 官网例子的时候报 undefined method `gsub' for nil?

gsky · 2014年05月22日 · 最后由 gsky 回复于 2014年05月25日 · 2706 次阅读

在 ubuntu 执行 Wombat 官网例子:

require 'wombat'

Wombat.crawl do base_url "http://www.github.com" path "/"

headline xpath: "//h1" subheading css: "p.subheading"

what_is({ css: ".teaser h3" }, :list)

links do explore xpath: '//*[@id="wrapper"]/div[1]/div/ul/li[1]/a' do |e| e.gsub(/Explore/, "Love") end

search css: '.search' features css: '.features' blog css: '.blog' end end

结果报

按照http://stackoverflow.com/questions/13303113/undefined-method-gsub-for-nilnilclass-nomethoderror 里说的方法,在 e.gsub 前面加了一个判空也一样报错,有哪位能帮忙分析下原因吗? :)

貌似 github 主页结构已经和些那个 readme 时候有出入了 页面上已经没有id=wrapper的 div 之后class="wrapper"的 div

@algo31031 github 主页上的例子也是报这个错...哎 @_@!

首先,仅仅作为一个建议,日后发帖若含代码希望能格式化好 (具体方法可按“M”键查看更多 帮助。)

然后再说一下报错原因:基本上报错信息里已经写得比较明白了,是对nil调用了gsub, 回到代码里会知道应该是 explore 那一行没找到内容。至于为何找不到上个回复已经说得很明白了

最后简单帮你修正下,把 explore 那行用下面这个替换下试试 explore xpath: '//*[@class="wrapper"]/div[1]/div[1]/div[2]/ul/li[1]/a' do |e|

@algo31031 好的,谢谢。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号