繁体   English   中英

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

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

我已经从index.html文件复制了内容。 现在,我只想复制样式标签中的所有内容。 我怎样才能做到这一点?

 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.

您可以使用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

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

未经测试,请尝试

暂无
暂无

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

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