爬虫入门(最基础的)


public void searchBaiDu(){
        HttpGet get=new HttpGet();
        get.setURI(URI.create("http://www.baidu.com"));//设置URI
        //设置请求头
        get.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        get.setHeader("Accept-Encoding","gzip, deflate, br");
        get.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        get.setHeader("Cache-Control","max-age=0");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Cookie","BAIDUID=7FC01AD27CFE39EB7488207233D0EDC3:FG=1; BIDUPSID=7FC01AD27CFE39EB7488207233D0EDC3; PSTM=1510034843; BD_UPN=12314753; MCITY=-%3A; __cfduid=d97d26f7dd2a35f42c0c47898e0a207271524639730; BD_HOME=0; H_PS_PSSID=26525_1465_21090_20930");
        get.setHeader("Host","www.baidu.com");
        get.setHeader("Upgrade-Insecure-Requests","1");
        get.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36");

        String resp=null;
        //创建默认的CloseableHttpClient,也可以自定义,用HttpClients.custom().***,就可以
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            //得到返回的页面源码
            response = client.execute(get);
            //然后再转换成String类型
            resp = EntityUtils.toString(response.getEntity());
            System.out.println(resp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/yhch1024/article/details/81100425