Perl实现下载百度Top100歌曲

自从遇见一位Perl大神,本人就对Perl 倾心已久, 想征服它,一直以来没那个勇气,但是从现在开始我要征服它,希望有一天能成长为他那样的级别。

挥手间,强弩灰飞烟灭。

从现在开始吧。。

下面是我的处女作.

用到的知识点都是最基本的, 高深的还不会,还要慢慢积累,有什么不足之处,还望各位大神指点指点小弟。


需求: 发现想自动化下载网络上的一些歌曲,不用人工下载。

实现: 用最简单的perl语法实现

说明: 最多限定了下载100首。


注意:

my $list_url = "http://music.baidu.com/top/new";

#http://music.baidu.com/top/dayhot

这个地址可以变更成自己想要的。


___________________________________________________________________

#!/usr/bin/perl

# Date: 2014.02.13
# Author: arika
# Info: jinfei09@126.com

use strict;
use LWP::Simple;
use IO::File;

my $url_root = "http://music.baidu.com";
my $list_url = "http://music.baidu.com/top/new";
my $main_url_file = "urls.txt";
my $songItem = "songItem";
my $songDir = "song";
my $count = 0;
my $song_max = 100;

qx(mkdir -p $songItem $songDir);

sub get_song_url
{
    (my $all_url_file, my $all_song_url)= @_;
    open(IN, ">$all_url_file") or die "Can not open $all_url_file, because: $!";
    print "Begin downloading $all_song_url\n";
    print IN get($all_song_url);
    close IN;
}

sub start_get_url
{
    my $file = shift;
    open(IN, "<$file") or die "Can not open $file,$!";
    while(<IN>) {
        if(/songItem/) {
            $count++;

            my $one_song = ($_=~m/song-item-hook { 'songItem':(.*)}/);
            my $one_song_info = ($1 =~ m/{ 'sid': '(.*)', 'sname': '(.*)', 'author': '(.*)' }/);
            my $song_name = "$2_$3.mp3";
            my $song_url = "$url_root/song" . "/$1";
            my $song_key_file = "songItem/$1";
            my $song_local_path = "song/$song_name";
            open(FILE, ">$song_key_file") or die "Can not open $song_key_file:$!";
            print FILE get($song_url);
            close FILE;

            open(FILEHANDLE, "<$song_key_file") || die "Can not open $song_key_file: $!";
            while(<FILEHANDLE>){
                if(/data_url="(.*)"/){
                    open(FILESONG, ">$song_local_path") or die "Can not open $song_local_path:$!";
                    print ">>>>>>Begin download [$count][$song_name]\n";
                    print "URL[$count]: " . $1 . "\n";
                    print FILESONG get($1);
                    close FILESONG;
                    print "<<<<<<Success download\n\n";
                }
            }
            close FILEHANDLE;
        }
        if($count eq $song_max) {last;}
    }
    close IN;
}

&get_song_url($main_url_file, $list_url);
&start_get_url($main_url_file);
qx(rm -rf $main_url_file $songItem);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值