Nokogiri分割错误? [英] Nokogiri Segmentation fault?

查看:106
本文介绍了Nokogiri分割错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Railscast运行一个简单的Ruby脚本,一旦运行程序,就会出现以下Segmentation fault bug错误.我尝试卸载并重新安装Nokogiri和LibXML,但仍然没有任何操作.无论如何,有没有修复Ruby 1.87版本?也许那是问题所在?

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

这是我使用的代码:

#!/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

推荐答案

您正在强制运行Apple安装的Ruby,即Ruby 1.8.7:

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

#!/usr/bin/ruby -w

代替由RVM管理的您的红宝石之一.试试:

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

#!/usr/bin/env ruby -w

这样,如果您希望系统Ruby运行代码,则可以告诉RVM切换到它:

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

rvm use system

,它将响应:Now using system ruby.或者,您可以使用任何RVM托管的Ruby来运行代码:

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

如果您有RVM安装了1.8.7的实例,或者

if you had RVM install an instance of 1.8.7, or

rvm 1.9.2

rvm default

如果您为RVM设置了默认的Ruby,这总是一个好主意:

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

rvm use 1.9.2 --default

您可以查看Ruby RVM的版本受其控制:

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 ]

现在,转到您的实际代码,您有一个错误.尝试检索商品的价格时,您正在寻找错误的CSS,没有找到价格节点,获取了nil值,然后尝试从中获取text.改用它:

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\.]+/]

您的输出将类似于:


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

在更改了#!行并修复了price行之后,我在系统中使用Ruby 1.8.7以及RVM控制的1.8.7和1.9.2运行了您的代码,没有问题.

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.

这篇关于Nokogiri分割错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆