Nokogiri 分段错误?

发布于 2024-10-30 12:20:58 字数 1266 浏览 1 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-11-06 12:20:58

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

#!/usr/bin/ruby -w

而不是由RVM 管理的Ruby 之一。尝试:

#!/usr/bin/env ruby -w

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

rvm use system

它会响应: Now using system ruby​​. 或者,您可以使用以下任何一种RVM 管理 Rubies 来运行代码:

rvm 1.8.7

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

rvm 1.9.2

rvm default

为 RVM 设置了默认的 Ruby,这始终是一个好主意:

rvm use 1.9.2 --default

您可以检查 Ruby RVM 的版本已下它的控制:

$ rvm list

rvm rubies

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

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

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,没有任何问题。

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.

浪荡不羁 2024-11-06 12:20:58

这篇文章中描述的解决方案可能适合您:升级到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文