---
title: "ruby 杂记"
author: "xu_wccq"
date: "2009-01-05T14:17:42Z"
tags: []
slug: "ruby"
draft: false
lastmod: "2009-01-05T14:18:42Z"
id: 310273
url: "https://hooney.iteye.com/blog/310273"
---

匹配网站中的脚本<script src="public/ab.js"></script>,
并且按照其中的目录结构下载保存.


```ruby


require 'open-uri'

url = 'http://www.feyasoft.com'
root = '/root/Desktop/fey'

open(url) do |f|
   f.each do |l|
      if l =~ /<script\stype="text\/javascript"\ssrc="(.+)"><\/script>/
          file_path = $1
          if file_path =~ /\/(\w+\.js)/
            path, file = $`,$1
            fullpath = root + path
            system("mkdir -p #{fullpath}")
            open(url + file_path) do |res|
              File.open(root + file_path, 'w+') do |ff|
                ff.write(res.readlines)
              end
            end
          end
      end
   end
end


```