网络爬虫小白教程 (HttpClient)

本文介绍了Java网络爬虫的基础知识,包括HttpClient的使用,从创建环境到GET、POST请求,再到连接池和请求参数的配置。通过实例展示了如何抓取网页数据,阐述了学习网络爬虫的原因,如搜索引擎实现、大数据分析和SEO优化等。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.学习计划

  1. 入门程序
  2. 网络爬虫介绍
  3. HttpClient抓取数据
  4. Jsoup解析数据
  5. 爬虫案例

2.网络爬虫

网络爬虫(Web crawler),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本

2.1 爬虫入门程序

2.1.1 环境准备
  • JDK1.8
  • IntelliJ IDEA
  • IDEA自带的Maven
2.1.2 环境准备

创建Maven工程itcast-crawler-first并给pom.xml加入依赖

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
2.1.3 加入log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.logger.cn.itcast = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

### 输出信息到控制抬 ###

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
2.1.4 编写代码

编写最简单的爬虫,抓取传智播客首页:http://www.itcast.cn/

public class CrawlerFirst {
    public static void main(String[] args) throws IOException {
        //1.打开浏览器,创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.输入网址,发起get请求创建HttpGet对象
        HttpGet httpGet=new HttpGet("http://www.itcast.cn");
        //3.按回车发起请求,返回响应,使用HttpClient对象发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.解析响应获取数据
        //判断状态码是否是200
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity httpEntity = response.getEntity();
            String content = EntityUtils.toString(httpEntity, "utf-8");
            System.out.println(content);
        }
    }
}

测试结果:可以获取到页面数据

在这里插入图片描述

3.网络爬虫

3.1 网络爬虫介绍

在这里插入图片描述

3.2 为什么学网络爬虫

我们初步认识了网络爬虫,但是为什么要学习网络爬虫呢?只有清晰地知道我们的学习目的,才能够更好地学习这一项知识。在此,总结了4种常见的学习爬虫的原因:

1.可以实现搜索引擎

  • 我们学会了爬虫编写之后,就可以利用爬虫自动地采集互联网中的信息,采集回来后进行相应的存储或处理,在需要检索某些信息的时候,只需在采集回来的信息中进行检索,即实现了私人的搜索引擎。

2.大数据时代,可以让我们获取更多的数据源。

  • 在进行大数据分析或者进行数据挖掘的时候,需要有数据源进行分析。我们可以从某些提供数据统计的网站获得,也可以从某些文献或内部资料中获得,但是这些获得数据的方式,有时很难满足我们对数据的需求,而手动从互联网中去寻找这些数据,则耗费的精力过大。此时就可以利用爬虫技术,自动地从互联网中获取我们感兴趣的数据内容,并将这些数据内容爬取回来,作为我们的数据源,再进行更深层次的数据分析,并获得更多有价值的信息。

3.可以更好地进行搜索引擎优化(SEO)。

  • 对于很多SEO从业者来说,为了更好的完成工作,那么就必须要对搜索引擎的工作原理非常清楚,同时也需要掌握搜索引擎爬虫的工作原理。
  • 而学习爬虫,可以更深层次地理解搜索引擎爬虫的工作原理,这样在进行搜索引擎优化时,才能知己知彼,百战不殆。

4.有利于就业。

  • 从就业来说,爬虫工程师方向是不错的选择之一,因为目前爬虫工程师的需求越来越大,而能够胜任这方面岗位的人员较少,所以属于一个比较紧缺的职业方向,并且随着大数据时代和人工智能的来临,爬虫技术的应用将越来越广泛,在未来会拥有很好的发展空间。

4.HttpClient

  • 网络爬虫就是用程序帮助我们访问网络上的资源,我们一直以来都是使用HTTP协议访问互联网的网页,网络爬虫需要编写程序,在这里使用同样的HTTP协议访问网页。
  • 这里我们使用Java的HTTP协议客户端 HttpClient这个技术,来实现抓取网页数据。

4.1.GET请求

访问传智官网,请求url地址:

http://www.itcast.cn/
public class HttpGetTest {
    public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet对象,设置url访问地址
        HttpGet httpGet=new HttpGet("http://www.itcast.cn");
        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

注意:这里需要注释掉scope才可以打印日志

 <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <!--<scope>test</scope>-->
        </dependency>

请求结果

在这里插入图片描述

4.2.带参数的GET请求

在传智中搜索学习视频,地址为:

http://yun.itheima.com/search?keys=Java
public class HttpGetParamTest {
    public static void main(String[] args) throws Exception {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置请求地址是http://yun.itheima.com/search?keys=Java
        //创建URI builder
        URIBuilder uriBuilder=new URIBuilder("http://yun.itheima.com/search");
        uriBuilder.setParameter("keys","Java");;
        //设置参数
        //创建httpGet对象,设置url访问地址
        HttpGet httpGet=new HttpGet(uriBuilder.build());
        System.out.println("发起请求的信息"+httpGet);
        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

这里需要调大 Idea 控制台打印缓存, 打印所有日志

在这里插入图片描述请求结果
在这里插入图片描述

在这里插入图片描述

4.3.POST请求

使用POST访问传智官网,请求url地址:

http://www.itcast.cn/
 public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet对象,设置url访问地址
        HttpPost httpPost=new HttpPost("http://www.itcast.cn");
        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

请求结果:

在这里插入图片描述

4.4.带参数的POST请求

在传智中搜索学习视频,使用POST请求,url地址为:

http://yun.itheima.com/search

url地址没有参数,参数keys=java放到表单中进行提交

  public static void main(String[] args) throws Exception {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet对象,设置url访问地址
        HttpPost httpPost=new HttpPost("http://yun.itheima.com/search");
        //声明list集合,封装表单中的参数
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        //设置请求地址是http://yun.itheima.com/search?keys=Java
        params.add(new BasicNameValuePair("keys","Java"));
        //创建表单的Entity对象
        //第一个参数是封装好的表单数据,第二个参数是编码
        UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(params,"utf8");
        //设置表单的Entity对象到post请求中
        httpPost.setEntity(formEntity);
        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

请求结果
在这里插入图片描述

4.5.连接池

如果每次请求都要创建HttpClient,会有频繁创建和销毁的问题,可以使用连接池来解决这个问题。

测试以下代码,并断点查看每次获取的HttpClient都是不一样的。

public class HttpClientPoolTest {
    public static void main(String[] args) {
        //创建连接池管理器
        PoolingHttpClientConnectionManager cm =new PoolingHttpClientConnectionManager();
        //设置最大连接数
        cm.setMaxTotal(100);
        //设置每个主机的最大连接数
        cm.setDefaultMaxPerRoute(10);//主机--百度,新浪,搜狐等
        //使用连接池管理器发起请求
        doGet(cm);//1565
        doGet(cm);
    }

    private static void doGet(PoolingHttpClientConnectionManager cm) {
        //不是每次创建新的httpClient,而是从连接池中获取httpClient
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        HttpGet httpGet=new HttpGet("http://www.itcast.cn");
        CloseableHttpResponse response=null;
        try{
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                String content = EntityUtils.toString(entity,"utf8");
                System.out.println(content);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(response!=null){
                try {
                    response.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
                //不能关闭httpClient,由连接池管理httpClient
                //httpClient.close();
            }
        }

    }
}

4.6 请求参数

有时候因为网络,或者目标服务器的原因,请求需要更长的时间才能完成,我们需要自定义相关时间

public class HttpConfigTest {
    public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpGet对象,设置url访问地址
        HttpGet httpGet=new HttpGet("http://www.itcast.cn");

        //配置请求信息
        RequestConfig config=RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间单位是毫秒
                             .setConnectionRequestTimeout(500) //设置获取连接的最长时间,单位毫秒
                             .setSocketTimeout(10*1000)//设置数据传输的最长时间,单位是毫秒
                             .build();
        //给请求设置请求信息
        httpGet.setConfig(config);

        CloseableHttpResponse response=null;
        try{
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response.getStatusLine().getStatusCode()==200){
                //获取响应实体
                HttpEntity entity = response.getEntity();
                //将实体转成string对象
                String content = EntityUtils.toString(entity,"utf-8");
                System.out.println(content.length());
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                //关闭response
                response.close();
                httpClient.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

核心代码
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值