Nokogiri Segmentation fault?

梦想与她 提交于 2019-12-22 07:25:58

问题


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

回答1:


You are forcing the Apple-installed Ruby to run, which is Ruby 1.8.7:

#!/usr/bin/ruby -w

instead of one of your Rubies managed by RVM. Try:

#!/usr/bin/env ruby -w

That way, if you want your system Ruby to run the code, you can tell RVM to switch to it:

rvm use system

and it will respond with: Now using system ruby. Alternately, you can use any of the RVM managed Rubies to run the code:

rvm 1.8.7

if you had RVM install an instance of 1.8.7, or

rvm 1.9.2

or

rvm default

if you set up a default Ruby for RVM, which is always a good idea:

rvm use 1.9.2 --default

You can check to see what versions of Ruby RVM has under its control:

$ rvm list

rvm rubies

   ruby-1.8.7-p334 [ x86_64 ]
=> ruby-1.9.2-p180 [ x86_64 ]

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 the text from it. Use this instead:

price = item.at_css(".camelPrice").text[/\$[0-9\.]+/]

Your output will look similar to:

Fisher-Price Power Wheels Batman Lil Quad Ride-On
 - $59.97
/ip/Fisher-Price-Batman-Lil-Quad/10098697

After making the change to the #! line, and the fix to the price 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.




回答2:


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



来源:https://stackoverflow.com/questions/5575981/nokogiri-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!