博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
httpClient get方式抓取数据
阅读量:4608 次
发布时间:2019-06-09

本文共 1944 字,大约阅读时间需要 6 分钟。

/*

     * 爬取网页信息
     */
    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;
        }
        
    }

转载于:https://www.cnblogs.com/lixiuming521125/p/7058577.html

你可能感兴趣的文章
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>
20145202马超《JAVA》预备作业1
查看>>
云推送注意(MSDN链接)
查看>>