易截截图软件、单文件、免安装、纯绿色、仅160KB

用ruby写的web抓取小工具

最近由于学习使用linux下的C开发,需要查询Linux C函数参考,就经常上http://man.chinaunix.net/develop/c&c++/linux_c/default.htm查看,描述得比较详细而且还有例子。
网上还有许多各种技术的网页格式的参考材料都非常强大,可惜很多时候都没有网。于是就想写个脚本可以把文档下载,像android开发者文档一样弄到本地来,用起来就方便多了,要是用CHM工具打成chm格式就更好了。
于是就动手用ruby写了这么个脚本,把网页读取下来,保存到本地,同时下载网页中的样式、JS文件以及图片等文件,并替换相应的链接,尽量保持网页的效果。整个程序大约300行,尝试地抓取了几个网站都挺好,保留了样式、超链接甚至部分的JS。除此之外,它还能限制下载文件的类型和大小,可以……
当然,bug还很多,而且网页的HTML内容过于灵活和复杂,很多东西搞下来可能就面目全非了,真正要写一个好的parser果然是相当复杂- -!
下面是代码的注释,完整文件可在此下载。
 
  
 #定义一个模块,打印日志
module Log
def Log.i(msg)
print "[INFO]#{msg}\n"
end
def Log.e(msg)
print "[ERROR]#{msg}\n"
end
end
#定义一个类,用于封装加入到队列中的url,包含了url的链接深度和待保存的文件名的信息
class UrlObj
attr_accessor :url,:depth,:save_name
def initialize(url,depth,save_name)
@url=url
@save_name=save_name
@depth=depth
end
def info#解析得到url中的信息
return @info if @info
begin
@info=URI.parse(@url)
return @info
rescue Exception=>e
Log.e e
return nil
end
end
def domain#获取url中的域名
if @domain
return @domain
elsif !info
Log.e("#{@url}'s host is nil")
return nil
elsif !info.host
Log.e("#{@url}'s host is nil")
return nil
else
return info.host[/\w+\.\w+$/]
end
end
end
接下来是最主要的一类WebSnatcher:
class WebSnatcher
THREAD_SLEEP_TIME=0.5#定义常量,线程睡眠时间
HTTP_RETRY_TIMES=3#设置打开链接的重试次数
#初始化类变量和各种设置参数
def initialize(opt={})
@url_to_download=[]#待下载的url容器
@url_to_download_count=0#待下载的url数量


相关文档:

Ruby学习笔记三——类


#一、定义一个类
class Person
  def initialize(name,age=18)
    @name=name;
    @age=age;
    @motherland="china";
  end
  def talk
    puts "my name is "+@name+" and I am "+@age.to_s
   &nb ......

Ruby学习笔记四——模块


#一、模块定义及引用,模块就是一段代码,里面有一些方法放一起。
#定义模块用module...end 。模块与类非常相似,但是:
#A) 模块不可以有实例对象;
#B) 模块不可以有子类。
include Math
puts sqrt(91);
module Me
  def sqrt(a)
  puts a*a;
  return a*a;
end
PI=3.1415926 ......

phpRPC + Ruby + Arduino = 远程控制

phpRPC + Ruby + Arduino = 遠程控制LED開關(?)
嗯,我知道這是個很無聊的Sample :P
關於phpRPC與Arduino請自行Google
觀看此demo之前請先安裝另外一篇所提到的serialport
與另外一個Gem
套件:phprpc
在這個ļ ......

菜鸟和RMXP和Ruby语言

      喜欢玩阿月系列RPG游戏的我,跟不少童鞋一样下了RPGMakerXP来瞻仰一下。当时觉得这个软件太方便了,用鼠标随便点点就能弄出来个有趣的小游戏。因为里面不仅自带不少地图和人物行走图等素材,容易上手的操作方式也让自己大有成就感……可是游戏毕竟是离不开编程,解开游戏包,看 ......

Fix Thrift 0.2.0 Installation with Ruby 1.9.1

When I try the command "gem install thrift" with Ruby 1.9.1, I got a compilation error with something related to a C function "strlcpy()".
Then I searched the web. It seems I am not alone and the community know it.
However I do not want to wait for official update, I want to ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号