简体   繁体   English

如何提取<style> content from HTML file in Ruby?

[英]How to extract <style> content from HTML file in Ruby?

I've copied contents from a index.html file. 我已经从index.html文件复制了内容。 Now I just want to copy everything that's inside the style tags. 现在,我只想复制样式标签中的所有内容。 How can I do this? 我怎样才能做到这一点?

 file = File.open("filepath/index.html", "rb")
 @html_file_contents = file.read  //@html_file_contents has raw html from which I need to extract style tag contents.

You can use Nokogiri gem 您可以使用Nokogiri宝石

require 'nokogiri'

file = File.open("filepath/index.html", "rb")
page = Nokogiri::HTML(file.read)
first_style_tag = page.css('style')[0]
puts first_style_tag.text

see this tutorial http://ruby.bastardsbook.com/chapters/html-parsing/ 请参阅本教程http://ruby.bastardsbook.com/chapters/html-parsing/

Not tested, please try it out 未经测试,请尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM