What is the best way to parse a web page in Ruby?

前端 未结 6 1820
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 08:41

I have been looking at XML and HTML libraries on rubyforge for a simple way to pull data out of a web page. For example if I want to parse a user page on stackoverflow how

6条回答
  •  北海茫月
    2020-12-24 09:30

    it seems to be an old topic but here is a new one. Example getting reputation:

    #!/usr/bin/env ruby
    
    require 'rubygems'
    require 'hpricot'
    require 'open-uri'
    
    user = "619673/100kg"
    html = "http://stackoverflow.com/users/%s?tab=reputation"
    
    page = html % user
    puts page
    
    doc = Hpricot(open(page))
    pars = Array.new
    doc.search("div[@class='subheader user-full-tab-header']/h1/span[@class='count']").text.each do |p|
      pars << p
    end
    
    puts "reputation " + pars[0]
    

提交回复
热议问题