簡體   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