java爬蟲03

public static void main(String[] args) {post

        Integer count = 20;fetch

        CloseableHttpClient httpclient = HttpClients.createDefault();spa

        HttpPost post = new HttpPost("http://www.oschina.net/fetch_tweets");.net

        List<NameValuePair> nvps = null;code

        try {orm

            for (int i = 1; i <= count; i++) {vps

                nvps = new ArrayList<NameValuePair>();圖片

                nvps.add(new BasicNameValuePair("p", i+""));utf-8

                post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));ci

                CloseableHttpResponse response = httpclient.execute(post);

                String document = EntityUtils.toString(response.getEntity(),"utf-8");

                Document doc = Jsoup.parse(document);

                Elements elements = doc.select("p[class=txt]");

                for (Element element:elements) {

                    System.out.println(element.text());

                }

            }

        }catch(Exception e){

            e.printStackTrace();

        } finally {

            if (post!=null) {

                try {

                    post.clone();

                } catch (CloneNotSupportedException e) {

                    e.printStackTrace();

                }

            }

            if (httpclient!=null) {

                try {

                    httpclient.close();

                } catch (IOException e) {

                    e.printStackTrace();

                

            }

        }

    }

2. [圖片] QQ截圖20141120145242.png    

相關文章
相關標籤/搜索