注册 登录
编程论坛 JavaScript论坛

java爬虫

飞逝的流年 发布于 2021-12-04 13:06, 1877 次点击
大佬们,为什么这个代码爬出来的还是网页代码

程序代码:
public class CrawlerGetTest {
    public static void main(String[] args)  {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建HttpGet请求
        HttpGet httpGet = new HttpGet("http://www.);

                CloseableHttpResponse response = null;
        try {
            //使用HttpClient发起请求
            response = httpClient.execute(httpGet);

            //判断响应状态码是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                //如果为200表示请求成功,获取返回数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                //打印数据长度
                System.out.println(content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放连接
            if (response == null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

            }
        }
    }
}
1 回复
#2
apull2021-12-04 15:24
你这网址爬出来就应该是个网页代码呀。
1