吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3713|回复: 5
收起左侧

[其他转载] <font color=blue>Java爬虫代码 helloworld级别</font>

[复制链接]
jiangchuankyo 发表于 2017-2-25 19:41
public static void main(String[] args) {
                        //请求的url(前面加http:// 不然会报错)

                String url="http://www.baidu.com";      

                //1.创建HttpClient对象 这里使用默认的配置的httpclient

                CloseableHttpClient client = HttpClients.createDefault();

                //2.创建某种请求方法的实例。这里使用get方法

                HttpGet httpGet = new HttpGet(url);

                InputStream inputStream = null;

                CloseableHttpResponse response = null;

                try {

                    //3.执行请求,获取响应

                    response = client.execute(httpGet);

                    //看请求是否成功,这儿打印的是http状态码

                    System.out.println(response.getStatusLine().getStatusCode());

                    //4.获取响应的实体内容,就是我们所要抓取得网页内容

                    HttpEntity entity = response.getEntity();

                    //5.将其打印到控制台上面,这里使用EntityUtils(也可以用inputStream)

                    if (entity != null) {

                        System.out.println(EntityUtils.toString(entity, "utf-8"));

                    }

                    EntityUtils.consume(entity);           

                    

                } catch (IOException e) {

                    e.printStackTrace();

                } finally {

                                //6.关闭连接,释放资源(很重要)

                    if (inputStream != null) {

                        try {

                            inputStream.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

                    if (response != null) {

                        try {

                            response.close();

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    }

                }

            }

点评

http://www.52pojie.cn/thread-568638-1-1.html  发表于 2017-2-25 22:57

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

vale 发表于 2017-2-25 20:11
看看不说话了老哥哥
威武霸气潇洒涛 发表于 2017-2-25 20:16
fjflgc 发表于 2017-10-16 19:53
头像被屏蔽
king8222 发表于 2017-11-13 16:31
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-19 06:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表