0

我有一个应用程序,我必须在其中获取 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) 现在我想解析这些结果以获得结果链接、描述等....

如何获取加载到 WebView 中的数据?请提出正确的方法,我是通过 Google 搜索 API 完成的,它接受关键字并给出响应,然后我通过通用可用方法对其进行解析。所以,请提出一些不同的建议。

4

1 回答 1

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);
于 2014-08-07T08:44:22.177 回答