开发者

Is there a way to select html elements with a certain class with ruby?

If I use

require 'net/http'

source = Net::HTTP.get('stackoverflow.com', '/index.html')

to extract the source code from a url, is the开发者_如何学Gore a way, in ruby, to find all link elements with a certain class, and then to extract the href attribute of those urls and put them in an array? (I know how I would do this in javascript but not in ruby.)

Perhaps I do not want to use net/http?


Sounds to me like Nokogiri would be perfect for you.

require 'nokogiri'
require 'openuri'

doc = Nokogiri::HTML(open('http://stackoverflow.com/index.html'))
doc.xpath('//h3/a[@class="foo"]').each do |element|
  # do something with element
end


Use Mechanize and look into this page.


require 'open-uri'
require 'hpricot'

source = open('stackoverflow.com/index.html').read # get raw html

doc = Hpricot(source) # parse with Hpricot
links = doc.search("//a[@class~='foo_bar']").collect { |a| a[:href] } # search for all links with 'foo_bar' class and then collect array of links

NB: code is not optimized, so read Hpricot documentation if you'd like to improve it ;)


you can use HTML/XML parsers: nokogiri, mechanize


Hpricot is a nice XML/HTML parser you could use to do this.


Try searching Parsing HTML / DOM to look for relevant results. I'm sure there are a ton out there.

How to manipulate DOM with Ruby on Rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜