这是我设法解决问题的方法,可能对寻找它的人有所帮助。
searchResultsArray = [[NSMutableArray alloc]init];
NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('ires').innerHTML"];
NSLog(@"html:%@",html);
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray * elements = [doc searchWithXPathQuery:@"//ol[@id='rso']/li[@class='g']"];
if ([nextPageLinkArray count] == 0 || (current_page !=0 && current_page == [nextPageLinkArray count]-1))
{
NSString *htmlLinks = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('nav').innerHTML"];
NSLog(@"htmlLinks:%@",htmlLinks);
NSData *dataLinks = [htmlLinks dataUsingEncoding:NSUTF8StringEncoding];
TFHpple * docLinks = [[TFHpple alloc] initWithHTMLData:dataLinks];
NSArray * linksArr = [docLinks searchWithXPathQuery:@"//tbody/tr/td/a[@class='fl']"];
NSLog(@"cnt:%d", [linksArr count]);
for (int i=0; i<[linksArr count]; i++)
{
NSMutableDictionary * temPdict = [[NSMutableDictionary alloc]init];
TFHppleElement * element3 = [linksArr objectAtIndex:i];
NSString * url = [element3 objectForKey:@"href"];
[temPdict setValue:url forKey:@"next_url"];
NSString * pageNo = [element3 text];
[temPdict setValue:pageNo forKey:@"page_no"];
[nextPageLinkArray addObject:temPdict];
}
}
NSLog(@"nextPageLinks:%@",nextPageLinkArray);
[searchResultsArray removeAllObjects];
for (int i=0; i<[elements count]; i++)
{
NSMutableDictionary * temPdict = [[NSMutableDictionary alloc]init];
TFHppleElement * element = [elements objectAtIndex:i];
NSArray * childerens_VSC = [element childrenWithClassName:@"vsc"];
if ([childerens_VSC count]==0)
{
continue;
}
TFHppleElement * element1 = [childerens_VSC objectAtIndex:0];
NSArray * childerens_R = [element1 childrenWithClassName:@"r"];
//NSLog(@"childrenArr:%@",childrenArr);
TFHppleElement * element2 = [childerens_R objectAtIndex:0];
NSArray * childerens_L = [element2 childrenWithClassName:@"l"];
//NSLog(@"childrenArr1:%@",childrenArr1);
TFHppleElement * element3 = [childerens_L objectAtIndex:0];
NSString * url = [element3 objectForKey:@"href"];
[temPdict setValue:url forKey:@"url"];
//cite
NSArray * childrens_S = [element1 childrenWithClassName:@"s"];
TFHppleElement * element5 = [childrens_S objectAtIndex:0];
NSArray * childrens_F_KV = [element5 childrenWithClassName:@"f kv"];
TFHppleElement * element6 = [childrens_F_KV objectAtIndex:0];
NSArray * arr = [element6 childrenWithTagName:@"cite"];
TFHppleElement * element7 = [arr objectAtIndex:0];
NSString * cite = [element7 text];
NSLog(@"cite:%@",cite);
[temPdict setValue:cite forKey:@"cite"];
[searchResultsArray addObject:temPdict];
}
NSLog(@"searchResults:%@",searchResultsArray);