[英]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.