zengyangzhiit
itjava2017
采纳率88.9%
2017-06-20 09:20 阅读 6.9k
已采纳

HttpClient请求返回结果对象HttpEntity处理

HttpClient请求返回结果对象HttpEntity处理转成字符串一串图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    strutce 丵鹰 2017-06-20 09:33

    多次使用了EntityUtils.toString了

    点赞 1 评论 复制链接分享
  • joy460154849 joy460154849 2017-06-20 09:33

    class VarietyUtil1 implements Runnable {

    public static Variety getHotVariety() {
        String url = "http://top.baidu.com/buzz?b=19&c=3&fr=topcategory_c3";
        String html = pickData(url);
        Variety v = analyzeHTMLByString(html);
        ConnectionMongo cm = new ConnectionMongo();
        cm.insertVariety(v);
        return v;
    }
    
    /*
     * 爬取网页信息
     */
    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;
        }
    
    }
    
    @Override
    public void run() {
        String url = "http://top.baidu.com/buzz?b=19&c=3&fr=topcategory_c3";
        String html = pickData(url);
        Variety v = analyzeHTMLByString(html);
        ConnectionMongo cm = new ConnectionMongo();
        java.util.Calendar c = java.util.Calendar.getInstance();
        v.setAddTime(c.getTime());
        cm.insertVariety(v);
        System.out.println(v);
    }
    

    }

    public class VarietyUtil {
    public static void main(String[] args) {
    VarietyUtil1 t = new VarietyUtil1();
    DateSwitch ds = new DateSwitch();
    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
    service.scheduleAtFixedRate(t, ds.HowOfen(), 86400, TimeUnit.SECONDS);
    }
    }

    点赞 1 评论 复制链接分享
  • Nxl19980606 巨人老王 2020-12-24 16:11

    isRepeatable()

    这个方法可以用于显示该 Entity是否可重复消费,设置不是重复消费

    点赞 评论 复制链接分享

相关推荐