我有一个应用程序,我将不得不获取LOADED webView的数据,我怎么能得到它? 让我详细解释一下:
1)我有一个链接'www.google.com'
2)我输入了关键字'问题'并生成了一个类似的链接, https://www.google.co.in/#hl=en&sclient=psy-ab&q=%@&oq=question&gs_l=serp.3..0i10l4.90108.93059.2.93802.10.8.2.0.0.0.171.1173.0j8.8.0...0.0...1c.1.8.psy-ab.6SJ9cs6717Q&pbx=1&bav=on.2,or.r_cp.r_qf.&fp=27feaf18f129334d&biw=1901&bih=383
3)在使用上述URL创建请求后,我在WebView中打开。
NSString * urlStr = [NSString stringWithFormat:@"https://www.google.co.in/#hl=en&sclient=psy-ab&q=%@&oq=question&gs_l=serp.3..0i10l4.90108.93059.2.93802.10.8.2.0.0.0.171.1173.0j8.8.0...0.0...1c.1.8.psy-ab.6SJ9cs6717Q&pbx=1&bav=on.2,or.r_cp.r_qf.&fp=27feaf18f129334d&biw=1901&bih=383"];
NSURL * gotoUrl = [NSURL URLWithString:urlStr];
NSURLRequest * requestP = [NSURLRequest requestWithURL:gotoUrl cachePolicy:NSURLCacheStorageAllowed timeoutInterval:60];
[webVw loadRequest:requestP];
4)我得到了一些搜索结果,加载到webView中,
5)现在我想解析这些结果以获得结果LINKS,DESCRIPTION等....
如何在WebView中加载数据?请建议正确的方法, 我是通过谷歌搜索API来做的,它接受关键字并给出响应,然后我通过一般可用的方法解析它。所以,请提出不同的建议。
答案 0 :(得分:0)
这是我设法解决问题的方法,可以帮助有人寻找它。
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);