Nokogiri 分段错误?
我正在尝试从 Railscast 运行一个简单的 Ruby 脚本,一旦运行我的程序,我就会收到以下 Segmentation failure bug 错误。我尝试卸载并重新安装 Nokogiri 和 LibXML,但仍然没有任何结果。有没有办法修复 Ruby 1.87 版本?也许这就是问题所在?
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
/Users/da/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/nokogiri.bundle:
[BUG] Segmentation fault ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Abort trap $ ruby -v ruby 1.9.2p180
(2011-02-18 revision 30909)
[x86_64-darwin10.7.0] $ bundle exec
nokogiri -v—
--- warnings: []
nokogiri: 1.4.4
ruby:
version: 1.9.2
platform: x86_64-darwin10.7.0
engine: ruby
libxml:
binding: extension
compiled: 2.7.7
loaded: 2.7.7
这是我使用的代码:
#!/usr/bin/ruby -w
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".item").each do |item|
title = item.at_css(".prodLink").text
price = item.at_css(".PriceCompare .BodyS, .PriceXLBold").text[/\$[0-9\.]+/]
puts "#{title} - #{price}"
puts item.at_css(".prodLink")[:href]
end
I am trying to run a simple Ruby script from Railscast and once I run my program I get the following Segmentation fault bug error. I have tried uninstalling and reinstalling Nokogiri and LibXML and still nothing. Is there anyway to fix the Ruby 1.87 version? Maybe that is the problem?
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
/Users/da/.rvm/gems/ruby-1.9.2-p180/gems/nokogiri-1.4.4/lib/nokogiri/nokogiri.bundle:
[BUG] Segmentation fault ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Abort trap $ ruby -v ruby 1.9.2p180
(2011-02-18 revision 30909)
[x86_64-darwin10.7.0] $ bundle exec
nokogiri -v—
--- warnings: []
nokogiri: 1.4.4
ruby:
version: 1.9.2
platform: x86_64-darwin10.7.0
engine: ruby
libxml:
binding: extension
compiled: 2.7.7
loaded: 2.7.7
This is the code I used:
#!/usr/bin/ruby -w
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".item").each do |item|
title = item.at_css(".prodLink").text
price = item.at_css(".PriceCompare .BodyS, .PriceXLBold").text[/\$[0-9\.]+/]
puts "#{title} - #{price}"
puts item.at_css(".prodLink")[:href]
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在强制运行Apple 安装的Ruby,即Ruby 1.8.7:
而不是由RVM 管理的Ruby 之一。尝试:
这样,如果您希望系统 Ruby 运行代码,您可以告诉 RVM 切换到它:
它会响应:
Now using system ruby.
或者,您可以使用以下任何一种RVM 管理 Rubies 来运行代码:如果您让 RVM 安装了 1.8.7 的实例,或者
您
为 RVM 设置了默认的 Ruby,这始终是一个好主意:
您可以检查 Ruby RVM 的版本已下它的控制:
现在,转向您的实际代码,您遇到了一个错误。当尝试检索商品的价格时,您正在寻找错误的 CSS,没有找到价格节点,获取
nil
值,然后尝试从中获取text
它。使用此替代:您的输出将类似于:
在对
#!
行进行更改并修复price
行后,我使用 Ruby 1.8 运行了您的代码.7 在我的系统中,以及 RVM 控制的 1.8.7 和 1.9.2,没有任何问题。You are forcing the Apple-installed Ruby to run, which is Ruby 1.8.7:
instead of one of your Rubies managed by RVM. Try:
That way, if you want your system Ruby to run the code, you can tell RVM to switch to it:
and it will respond with:
Now using system ruby.
Alternately, you can use any of the RVM managed Rubies to run the code:if you had RVM install an instance of 1.8.7, or
or
if you set up a default Ruby for RVM, which is always a good idea:
You can check to see what versions of Ruby RVM has under its control:
Now, moving to your actual code, you have a bug. When trying to retrieve the price for an item you're looking for the wrong CSS, not finding the price node, getting a
nil
value, then trying to get thetext
from it. Use this instead:Your output will look similar to:
After making the change to the
#!
line, and the fix to theprice
line, I ran your code using Ruby 1.8.7 in my system, along with RVM controlled 1.8.7 and 1.9.2 with no problems.这篇文章中描述的解决方案可能适合您:升级到 ruby 1.9.2 并在 nokogiri 中出现分段错误
May be the solution described in this post will work for you: Upgraded to ruby 1.9.2 and getting Segmentation Fault errors in nokogiri