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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8938|回复: 25
收起左侧

[Java 转载] Java爬虫代码(helloworld级别)

  [复制链接]
NullPointer 发表于 2016-12-28 18:02
本帖最后由 NullPointer 于 2016-12-28 18:04 编辑

[Asm] 纯文本查看 复制代码
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();
                        }
                    }
                }
            }

免费评分

参与人数 4吾爱币 +1 热心值 +4 收起 理由
尾叶 + 1 + 1 谢谢@Thanks!
一介书生 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
温馨提示 + 1 楼主,需要包
zby03772015 + 1 谢谢@Thanks!

查看全部评分

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

 楼主| NullPointer 发表于 2016-12-29 13:52
testcat 发表于 2016-12-29 09:40
Java实现爬虫的框架还是较少一点  个人还是喜欢用Python

py确实爬虫略叼
 楼主| NullPointer 发表于 2016-12-29 14:55
Unreal.E.Age 发表于 2016-12-29 14:02
。。。也不就是个查看网页源码吗

看标题,helloworld级别。只是带大家入门
m0216 发表于 2016-12-28 18:23
 楼主| NullPointer 发表于 2016-12-28 18:42 来自手机
m0216 发表于 2016-12-28 18:23
没看懂。。。。。。。。

需要一小点httpclient的知识
林深时见鹿丶 发表于 2016-12-28 18:46
包呢? 怎么没有包
头像被屏蔽
屌絲钕 发表于 2016-12-28 18:48
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| NullPointer 发表于 2016-12-28 18:53 来自手机
林深时见鹿丶 发表于 2016-12-28 18:46
包呢? 怎么没有包

抱歉,下班了,明天我补上
mandarin 发表于 2016-12-28 20:17
这是你写的爬虫类麽
 楼主| NullPointer 发表于 2016-12-28 20:18
mandarin 发表于 2016-12-28 20:17
这是你写的爬虫类麽

helloworld级别的
快速路丶 发表于 2016-12-28 20:27
这是干什么的?
ACGN小角虫 发表于 2016-12-28 20:28
哦摩西罗伊~~~~~
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-20 07:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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