zoukankan      html  css  js  c++  java
  • httpClient get方式抓取数据

    /*
         * 爬取网页信息
         */
        private static String pickData(String url) {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpGet httpget = new HttpGet(url);
                CloseableHttpResponse response = httpclient.execute(httpget);
                try {
                    // 获取响应实体
                    HttpEntity entity = response.getEntity();
                    // 打印响应状态
                    if (entity != null) {
                        InputStream in = entity.getContent();
                        // byte[] b=new byte[in.available()];
                        // in.read(b);
                        BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
                        String temp = "";
                        String s = "";
                        while ((temp = br.readLine()) != null) {
                            s = s + temp;
                        }
                        return s;
                    } else {
                        String content = "热门综艺节目抓取失败,请检查";
                        ErrorLog el = new ErrorLog();
                        Remind remind = new Remind();
                        remind.remind(el.getVerietyLog(), content);
                        return null;
                    }
                } finally {
                    response.close();
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        /*
         * 使用jsoup解析网页信息
         */
        private static Variety analyzeHTMLByString(String html) {
            Variety v = new Variety();
            String[] arr = new String[3];
            Document document = Jsoup.parse(html);
            // document.select("meta").attr("charset", "utf-8");
            // System.out.println(document);
            Elements array = document.getElementsByClass("keyword");
            System.out.println(array.size());
            String content = "热门综艺节目抓取失败,请检查";
            ErrorLog el = new ErrorLog();
            if (array.size() == 0) {
                Remind remind = new Remind();
                remind.remind(el.getVerietyLog(), content);
                return null;
            }else{
                if (array.size() >= 3) {
                    for (int i = 0; i < 3; i++) {
                        String name = array.get(i).child(0).text();
                        arr[i] = name;
                    }
                } else {
                    for (int i = 0; i < array.size(); i++) {
                        String name = array.get(i).child(0).text();
                        arr[i] = name;
                    }
                }
                v.setHot1(arr[0]);
                v.setHot2(arr[1]);
                v.setHot3(arr[2]);
                return v;
            }
            
        }

  • 相关阅读:
    【字符集及字符编码】UTF-8、UTF-16和UTF-32
    【Android】SQLite基本用法(转)
    【eclipse】导入/导出开发环境(包括编辑器字体颜色大小等)
    一个Android Socket的例子(转)
    Linux中与Windows对应的InterlockedIncrement()函数
    Linux互斥锁pthread_mutex_t
    C++读写文本文件
    C++回调函数调用Java接口抽象函数
    Overlapped I/O模型--事件通知【摘录自《Windows网络编程》】
    Linux C++中需要的头文件
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/7058577.html
Copyright © 2011-2022 走看看