
最近在CSDN的一个blog上看到一篇网络小说连载《疯狂的程序员》,看了头几篇来了兴趣,就准备都弄下来放到手机上看。小说到现在连载到50多集,前面试读的几篇都是手工复制到文本文件,虽然说手工50多次ctrl+c, ctrl+v也不是特别麻烦,但学过编程的人心理就是会觉得做这档事会不爽^^ 于是就决定用ruby写个爬虫程序来搞定这个小任务。
相关链接:
ruby的html实体转换库:HTMLEntities(http://htmlentities.rubyforge.org/)
String.scan()文档:http://www.ruby-doc.org/core/classes/String.html#M000827
String.gsub!()文档:http://www.ruby-doc.org/core/classes/String.html#M000839
源代码压缩包中有两个文件,csdn_crawler.rb是视频中用到的代码,csdn_crawler2.rb是在csdn_crawler.rb基础上处理了一写额外问题的,并加入注释,方便阅读。
09月 1st, 2008 at 17:36:10 #z
运行csdn_crawler.rb,就保存了一篇,但文件名为.txt,改为UTF-8也不行,出现下面信息,如何解决?PS:windows下已经装完:gem install htmlentities
C:\InstantRails-2.0-win\ruby\lib\ruby\1.8>ruby csdn_crawler.rb
C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:278:in `open_http': 400 Ba
d Request (OpenURI::HTTPError)
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:617:in `buffe
r_open'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:164:in `open_
loop'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:162:in `catch
'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:162:in `open_
loop'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:132:in `open_
uri'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:519:in `open'
from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/open-uri.rb:30:in `open'
from csdn_crawler.rb:9
[回复]
09月 2nd, 2008 at 11:49:30 #jarod
用浏览器确认一下你是不是可以访问到文章的地址
[回复]
09月 3rd, 2008 at 09:08:22 #z
可以阿
就保存了一篇有内容,但文件名为.txt
[回复]
09月 3rd, 2008 at 18:16:09 #jarod
csdn的blog应该是改过版了,各个正则式要改一下才能用,你就锻炼一下自己吧^^
[回复]
09月 9th, 2008 at 17:43:44 #z
Thanks,你重试了,也不行了吗?
我刚学RoR,有时间我再看看。
[回复]