jsoup-Elements的遍历(使用Iterator迭代器)

    public void crawlingData(){
        String url = "http://……";
        HttpClientBuilder hcb = HttpClientBuilder.create();
        CloseableHttpClient hc = hcb.build();
        HttpUriRequest get = new HttpGet(url);          
        CloseableHttpResponse responseCode;
        try {
            responseCode = hc.execute(get);
            HttpEntity dataEnt = responseCode.getEntity();      
            String htmlString = EntityUtils.toString(dataEnt,"utf-8");
            Document htmlDocument = Jsoup.parse(htmlString);            
            Elements elements = htmlDocument.getElementsByTag("tr");
            //获取迭代器 
            Iterator it = elements.iterator();          
            while(it.hasNext()) {  
                Element element = (Element)it.next();
                Node titleNode= element.child(0).childNode(0);              
                //遍历中。。。。。。 
            }   
        } catch (ClientProtocolException e) {
            System.out.println("详情页网络连接出错:"+url);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("访问详情页出错:"+url);
            e.printStackTrace();
        }
}

你可能感兴趣的:(工作所学)