大家好,又见面了,我是全栈君。
试了一下。基本上适合全部的检索结果。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取数据
NSURL *url=[NSURL URLWithString:@"http://s.g.wanfangdata.com.cn/Paper.aspx?q=hiv"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//解析数据
TFHpple *hpple=[TFHpple hppleWithHTMLData:data];
//XPATH字符串 越短越好。可是要能过滤出符合要求的结果
//xpath教程:http://www.w3cschool.cn/index-14.html
NSString *xpathStr =@"//div/ul/li [@class='title_li'] /a [not(@class)]";//解析标题
NSString *xpathStr2 =@"//div/ul/li [@class='zi'] ";//解析简单介绍
NSArray *arr=[hpple searchWithXPathQuery:xpathStr];
NSArray *arr2=[hpple searchWithXPathQuery:xpathStr2];
//NSLog(@"%@",arr2);
//这个数组中就有须要的值
alltilte=[[NSMutableArray alloc]init];
alltext=[[NSMutableArray alloc]init];
alltilte = [self SaveArry:arr];
alltext = [self SaveAlltext:arr2];
for(int i=0;i<alltilte.count;i++)
{
NSLog(@"标题%@",alltilte[i]);
NSLog(@"简单介绍%@",alltext[i]);
}
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
-(NSMutableArray *)SaveAlltext :(NSArray *)_arry
{
for (TFHppleElement *tmp in _arry)
{
NSString * string =[tmp raw];
NSRange range;
// NSRange range2;
range = [string rangeOfString:@"<br />"];
string = [string substringToIndex:range.location];
NSString *temp =[string stringByReplacingOccurrencesOfString:@"<li class=\"zi\">
"
withString:@""];
temp =[temp stringByReplacingOccurrencesOfString:@"<font color=\"red\">"
withString:@""];
temp =[temp stringByReplacingOccurrencesOfString:@"</font>"
withString:@""];
temp =[temp stringByReplacingOccurrencesOfString:@"<li class=\"zi\">"
withString:@""];
[alltext addObject:temp];
}
return alltext;
}
-(NSMutableArray *)SaveArry: (NSArray *)_arry
{
for (TFHppleElement *tmp in _arry)
{
NSString * string =[tmp raw];
NSRange range;
NSRange range2;
range = [string rangeOfString:@"</a>"];
string = [string substringToIndex:range.location];
NSString *temp =[string stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""];
temp=[temp stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
range = [string rangeOfString:@">"];
range2 = [string rangeOfString:@"img"];
if(range.length!=0&&range2.length==0)
{
string = [temp substringFromIndex:range.location+1];
[alltilte addObject:string];
}
}
return alltilte;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116261.html原文链接:https://javaforall.cn