• 使用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有所帮助,欢迎您根据本文提供的示例代码进行实践和探索。

  • 相关阅读:
    Tcl语言:SDC约束命令create_generated_clock详解(上)
    Web自动化测试--selenium
    【Rust 日报】2022-6-23 Jon Gjengset 的关键字小技巧系列
    居民消费价格指数变化新鲜出炉,这类商品同比涨幅最大
    Python数据结构——基础数据结构
    基于深度强化学习的智能汽车决策模型
    行业观察|车企数字化营销破局之道
    【软件安装】ubuntu+CGAL+QT可视化+draw_triangulation_2+draw_triangulation_3
    查找总价格为目标值的两个商品 ---- 双指针
    浅述数据中心供配电系统解决方案及产品选型
  • 原文地址:https://blog.csdn.net/hitpter/article/details/133378765