如何使用Ruby on Rails操作DOM [英] How to manipulate DOM with Ruby on Rails
问题描述
- 找到所有具有蓝色的H1元素。
- 找到所有大小为12px的文本。
- etc ..
如何使用Rails?
谢谢.. :)
更新
我一直在做一些关于提取网页的研究内容基于本文 - > http://www.springerlink.com/index/A65708XMUR9KN9EA.pdf
步骤总结如下:
- 获取网页我想要提取的URL(单个网页)
- 根据一些视觉规则从网页中获取一些元素(例如:抓住所有具有蓝色颜色的 H1 )
- 使用我的算法处理元素
- 将结果保存到我的数据库中。
-sorry for my bad english -
如果你想做是在rails应用程序中操作HTML文档,你应该看看 Nokogiri 。
它使用XPath搜索文档。以下,您将在文档中找到任何具有bluecss类的h1。
require'nokogiri'
require'open-uri'
doc = Nokogiri :: HTML(open('http://www.stackoverflow.com'))
doc.xpath('// h1 /a[@class=\"blue\"]').each do | link |
puts link.content
end
之后,如果你试图做的确是解析当前页面的dom,你应该看看JavaScript和JQuery。 Rails不能这样做。
As the title said, I have some DOM manipulation tasks. For example, I want to: - find all H1 element which have blue color. - find all text which have size 12px. - etc..
How can I do it with Rails?
Thank you.. :)
Update
I have been doing some research about extracting web page content based on this paper-> http://www.springerlink.com/index/A65708XMUR9KN9EA.pdf
The summary of the step is:
- get the web url which I want to be extracted (single web page)
- grab some elements from the web page based on some visual rules (Ex: grab all H1 which have blue color)
- process the elements with my algorithm
- save the result into my database.
-sorry for my bad english-
If what you're trying to do is manipulate HTML documents inside a rails application, you should take a look at Nokogiri.
It uses XPath to search through the document. With the following, you would find any h1 with the "blue" css class inside a document.
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.stackoverflow.com'))
doc.xpath('//h1/a[@class="blue"]').each do |link|
puts link.content
end
After, if what you were trying to do was indeed parse the current page dom, you should take a look at JavaScript and JQuery. Rails can't do that.
这篇关于如何使用Ruby on Rails操作DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!