- 論壇徽章:
- 0
|
經(jīng)過分析和訊的網(wǎng)頁信息,和訊是通過COOKIE防止非法外鏈,而真正影響音樂文件下載的COOLIE是由SetPlayState.js腳本生成的,原代碼如下:- //設(shè)置播放狀態(tài)Cookie值。
- function SetPlayState()
- {
- var cookieName = "HEXUN_COM_MEDIA_PLAYSTATE";
- var curCookie = cookieName + "=1;path=/;domain=hexun.com";
- document.cookie = curCookie;
- }
- SetPlayState();
復(fù)制代碼 那么在下載音樂文件的時候,需要在HTTP請求中加入這個COOKIE。
以ID號為5692541音樂為例:
它的播放網(wǎng)頁為:
http://fangchuanqian.music.hexun.com/M5692541.html
經(jīng)分析網(wǎng)頁代碼,它的網(wǎng)頁播放器鏈接為:
http://music.hexun.com/PlayMedia ... 692541&FileUrl=
再分析能發(fā)現(xiàn),真正生成MP3實(shí)際下載地址的鏈接為:
http://homemusic.tool.hexun.com/ ... 692541&FileUrl=
向“http://homemusic.tool.hexun.com/ ... 692541&FileUrl=”發(fā)送請求,并在請求中加入COOKIE信息,即可正常下載音樂文件。
寫了一個簡單的按音樂的ID號下載的代碼:- #!/usr/bin/perl
- use strict;
- use warnings;
- use LWP::UserAgent;
- use LWP::ConnCache;
- local $| =1;
- my @mp3_ID = ('5692541','5597643');
- my $ua = LWP::UserAgent->new;
- my $conncache = new LWP::ConnCache;
- $ua->conn_cache($conncache);
- my @head = ('Cookie'=>'HEXUN_COM_MEDIA_PLAYSTATE=1'); #將COOLIE加入GET請求頭
- print "\n Download begin ...\n\n";
- foreach my $id (@mp3_ID) {
- my $res1 = $ua->get("http://homemusic.tool.hexun.com/PM.aspx?CID=1&MID=$id&FileUrl=");
- my $html = $res1->as_string;
- if ($html =~ /歡迎訪問.*?和訊音樂個人門戶/) {
- print "ID $id is error!\n";
- }
- else {
- my ($mp3_url) = ($html =~ /A HREF="(.*?)"/i);
- print " Downloading $id.mp3 ... ";
- my $res2 = $ua->get($mp3_url,@head);
- if ($res2->is_success) {
- open(my $fp, ">:raw", "$id.mp3");
- print $fp $res2->content;
- close $fp;
- print "OK\n";
- }
- else { print "Failed!\n"; }
- }
- }
- print "\n All downloaded!\7";
- <STDIN>;
復(fù)制代碼 可以通過從和訊的網(wǎng)頁中批量提取音樂ID,加入@mp3_ID中,以實(shí)現(xiàn)批量下載。 |
|