• 使用Apache HttpClient爬取网页内容的详细步骤解析与案例示例


    Apache HttpClient是一个功能强大的开源HTTP客户端库,本文将详细介绍如何使用Apache HttpClient来爬取网页内容的步骤,并提供三个详细的案例示例,帮助读者更好地理解和应用。

    一、导入Apache HttpClient库

    在项目的pom.xml文件中添加依赖,将以下代码添加到pom.xml文件中:

    <dependency>
        <groupId>org.apache.httpcomponentsgroupId>
        <artifactId>httpclientartifactId>
        <version>4.5.13version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、创建爬虫类和HttpClient对象

    创建一个名为WebCrawler的Java类。

    • 使用HttpClients工具类的createDefault()方法创建一个默认的HttpClient对象,示例代码如下:
    CloseableHttpClient httpClient = HttpClients.createDefault();
    
    • 1

    三、创建HttpGet请求对象

    使用HttpGet的构造方法,传递网页URL作为参数来创建一个HttpGet请求对象,示例代码如下:

    HttpGet httpGet = new HttpGet("http://www.example.com");
    
    • 1

    四、发送请求并获取响应

    使用HttpClient的execute()方法发送请求并获取响应,该方法接收一个HttpGet对象作为参数,并返回一个CloseableHttpResponse对象,包含了服务器返回的响应信息,示例代码如下:

    CloseableHttpResponse response = httpClient.execute(httpGet);
    
    • 1

    五、提取网页内容

    使用EntityUtils工具类的toString()方法,将响应实体转换为字符串形式的网页内容,该方法接收一个HttpEntity对象作为参数,并返回一个字符串,示例代码如下:

    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    
    • 1

    六、打印网页内容并关闭响应和HttpClient

    使用System.out.println()方法打印出网页内容。

    • 调用response的close()方法关闭响应。
    • 调用httpClient的close()方法关闭HttpClient,示例代码如下:
    System.out.println(content);
    response.close();
    httpClient.close();
    
    • 1
    • 2
    • 3

    七、案例示例

    案例一:爬取某度首页内容

    HttpGet httpGet = new HttpGet("https://www.xxxxx.com");
    CloseableHttpResponse response = httpClient.execute(httpGet);
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    response.close();
    httpClient.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    案例二:爬取某乎热榜内容

    HttpGet httpGet = new HttpGet("https://www.xxxxx.com/hot");
    CloseableHttpResponse response = httpClient.execute(httpGet);
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    response.close();
    httpClient.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    案例三:爬取某瓣电影TOP250内容

    HttpGet httpGet = new HttpGet("https://movie.xxxxxx.com/top250");
    CloseableHttpResponse response = httpClient.execute(httpGet);
    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(content);
    response.close();
    httpClient.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意事项:

    • 设置请求间隔时间,避免对服务器造成过大的负载。
    • 处理异常情况,如网络连接失败、网页不存在等,使用try-catch语句来处理这些异常情况,并采取相应的措施。

    结语:

    通过以上步骤和案例示例,我们可以使用Apache HttpClient来爬取网页内容。Apache HttpClient提供了丰富的功能和配置选项,您可以根据具体的需求和情况进行相应的调整和扩展。希望本文对您了解和使用Apache HttpClient有所帮助,欢迎您根据本文提供的示例代码进行实践和探索。

  • 相关阅读:
    vuex 学习之路
    软设上午题错题知识点5
    MyCat的安装
    电解电容寿命与哪些因素有关?
    【服务器数据恢复】5节点Lustre分布式文件系统RAID5数据恢复案例
    记录--JavaScript原型和原型链复习笔记
    QT Model/View 编程:MVC模型视图编程:实例实现(二)
    电流继电器JL-8GB/11/AC220V
    python调用win32api 拉起wps问题排查解决方案
    【编译原理】实验一 词法分析器(Java实现)
  • 原文地址:https://blog.csdn.net/hitpter/article/details/133378765