播放器解析源代码(1)
#import"testerViewController.h"
#import"ASIHTTPRequest.h"
@implementationtesterViewController
+(NSArray*)KeyWordsForSongs:(NSString*)myKeyWordspageNavi:(int)myPageNavi{
int pageNavi = myPageNavi; //页码
NSString * keyWords = myKeyWords;//关键字
//NSString * keyWords = @"张信哲";
//创建字符串
NSString * myURL = [NSStringstringWithFormat:@"mp3.baidu/m?
f=ms&rf=idx&tn=baidump3&ct=134217728&lf=&rn=&word=%@&lm=0&pn=%d",keyWords,pageNavi];
/
/UTF8 to gb2312
myURL = [myURLstringByAddingPercentEscapesUsingEncoding:CFStringConvertEncodingT
oNSStringEncoding(kCFStringEncodingGB_18030_2000)];
NSLog(@"%@",myURL);
ASIHTTPRequest *myRequest = [[[ASIHTTPRequestalloc] initWithURL:[NSURL URLWithString:myURL]]autorelease];
[myRequest startSynchronous];
//如果发⽣错误,返回nil
if ([myRequest error]) return nil;
//返回数组定义
NSMutableArray * mySongs = [NSMutableArrayarrayWithCapacity:15];
//编码转换gb2313 to UTF
NSData * myResponseData = [myRequestresponseData];
NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString * myResponseStr = [[NSString alloc]initWithData:myResponseData encoding:enc];
//NSLog(@"html=%@",myResponseStr);
//如果百度告诉我没有到?
NSRange myRange;
myRange = [myResponseStrrangeOfString:@"抱歉,没有到与"];
if (myRange.location != NSNotFound) {
returnnil;
};
//将源代码按” <tdclass=d><a href= “切割
NSMutableArray * myResponseArrForSinger =(NSMutableArray *)[myResponseStrcomponentsSeparatedByString:@"<tdclass=d><a href="];
//去头去尾
if ([myResponseArrForSingercount]>=2) {
[myResponseArrForSinger removeObjectAtIndex:0];
[myResponseArrForSinger removeObjectAtIndex:[myResponseArrForSingercount]-1];
}
//按段处理
for (int i=0;i<[myResponseArrForSinger count]; i++) {
//
//把段按“ .html" target="_blank"> ”切割
NSMutableArray * mySubArr =(NSMutableArray *)[[myResponseArrForSinger objectAtIndex:i]componentsSeparatedByStr
ing:@".html"target="_blank">"];
//处理歌曲下载页⾯的URL
NSArray * myURLTmpArr =[[mySubArr objectAtIndex:0] componentsSeparatedByString:@""title="请点击左键!来源⽹址: "];
myURLTmpArr = [[mySubArrobjectAtIndex:0] componentsSeparatedByString:@"""];
NSMutableString * SongsURL =[myURLTmpArr objectAtIndex:1];
for (int j=0;j<[mySubArr count]; j++) {
NSLog(@"%d= %@",j,[mySubArrobjectAtIndex:j]);
}
//如果只有⼀段,表⽰⽆歌⼿名和专辑名
//if ([mySubArrcount]<=1) {
//NSLog()
//常量定义段的意义
const int Const_Songs =0;
const int Const_Singer =1;
const int Const_Album =4;
//变量定义
NSMutableString * SongsName =(NSMutableString*)@"";
NSString * SongsSinger =@"";
NSString * SongsAlbum =@"";
NSString * SongsSpeed =@"";
NSString * SongsSize =@"";
//处理歌曲⼤⼩
NSMutableArray * mySizeArr =(NSMutableArray*)[[mySubArr objectAtIndex:([mySubArr count]-1)]componentsSeparatedByString:@" <td>"];
if ([mySizeArrcount]>=2) {
SongsSize = [mySizeArr objectAtIndex:([mySizeArrcount]-2)];
NSLog(@"size = %@",SongsSize);
};
//处理歌曲链接速度图⽚
@try {
if ([mySubArr count]>=3){
if([mySubArr objectAtIndex:([mySubArr count]-1)]!=nil) {
NSMutableArray * mySpeedArr =(NSMutableArray*)[[mySubArr objectAtIndex:([mySubArrcount]-1)] componentsSeparatedByString:@"<tdclass=spd><imgsrc="img.baidu/img/mp3/"];
if ([mySpeedArrcount]>0)
if ([mySpeedArrobjectAtIndex:1]!=nil)
电影源代码 下载mySpeedArr= (NSMutableArray*)[[mySpeedArr objectAtIndex:1] componentsSeparatedByString:@""></td>"];
if ([mySpeedArrobjectAtIndex:0]!=nil)
SongsSpeed = [mySpeedArrobjectAtIndex:0];
NSLog(@"test speed =%@",SongsSpeed);
};
}
@catch (NSException * e){
//none
}
@finally {
//none
}
//处理歌曲名
NSMutableArray * myTmpArr =(NSMutableArray *)[[mySubArr objectAtIndex:Const_Songs]componentsSeparatedByStr ing:@""returnow(event,this)" target="_blank">"];
myTmpArr = (NSMutableArray*)[[myTmpArr objectAtIndex:1]componentsSeparatedByString:@"</a></td>"];
SongsName = [myTmpArrobjectAtIndex:0];
NSString *mySubStr;
//处理歌⼿名
if ([mySubArrcount]>1) {
mySubStr = [mySubArrobjectAtIndex:Const_Singer];
SongsSinger = mySubStr;
};
//处理专辑名
if ([mySubArrcount]>=5) {
mySubStr = [mySubArrobjectAtIndex:Const_Album];
NSMutableArray * mySubSubArr = (NSMutableArray*)[mySubStrcomponentsSeparatedByString:@"</a>"];
mySubStr = [mySubSubArrobjectAtIndex:0];
SongsAlbum = mySubStr;
} else {
SongsAlbum = @"";
};
待续...
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论