如何使用Ruby on Rails操作DOM [英] How to manipulate DOM with Ruby on Rails

查看:168
本文介绍了如何使用Ruby on Rails操作DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我有一些DOM操作任务。例如,我想:
- 找到所有具有蓝色的H1元素。
- 找到所有大小为12px的文本。
- etc ..



如何使用Rails?



谢谢.. :)



更新



我一直在做一些关于提取网页的研究内容基于本文 - > http://www.springerlink.com/index/A65708XMUR9KN9EA.pdf



步骤总结如下:


  1. 获取网页我想要提取的URL(单个网页)

  2. 根据一些视觉规则从网页中获取一些元素(例如:抓住所有具有蓝色颜色的 H1

  3. 使用我的算法处理元素

  4. 将结果保存到我的数据库中。

-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:

  1. get the web url which I want to be extracted (single web page)
  2. grab some elements from the web page based on some visual rules (Ex: grab all H1 which have blue color)
  3. process the elements with my algorithm
  4. 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屋!

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