Suninny 发表于 2013-1-15 03:07:18

实践百度加密链接解码并尝试Ruby抓取新歌Top100

 
配合之前写好了的Decode.rb,来个爬虫实践。
 
最新代码.tar.gz 打包下载
 
 
#...text = lambda { |x| unicode.iconv(x.inner_text) }link = lambda { |x| x[:href] }whats = open(WHATS) { |f| Hpricot(f) }singers = (whats/"/html/body/table/tr/td/a").collect &textsongs = (whats/"/html/body/table/tr/td/a")song_names = songs.collect &text song_urls = songs.collect &linkFile.open("top100_list.txt", "w") do |os|song_urls.each_with_index do |song, i|    #exit if i == 20 # 这里控制排行榜歌曲数   os << "#{i+1}. #{singers} - #{song_names}\n"    links = open(song){ |f| Hpricot(f) }    urls = (links/"/html/body/div/table/tr/td/a").collect &link   urls.each do |url| # 这里控制每首歌的链接数      page = Net::HTTP.get(URI.parse(URI.encode(url)))      os << decode(unicode.iconv($1)) << "\n" if page =~ /var .="(.*)",J/    end    os << "\n"endend  Hpricot的解析速度还是不错的。生成的文件内容(片断)如下:
<div class="quote_title"> 写道
页: [1]
查看完整版本: 实践百度加密链接解码并尝试Ruby抓取新歌Top100