Java网络爬虫入门

文章目录

1、导入依赖

xml 复制代码
<dependencies>
    <!-- HttpClient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>

    <!-- 日志 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
    </dependency>
</dependencies>

2、CrawlerFirst

java 复制代码
package com.atguigu.crawler.test;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class CrawlerFirst {
    public static void main(String[] args) throws IOException {
        //1、打开浏览器,创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2、输入网址,发起get请求,获取响应
        HttpGet httpGet = new HttpGet("https://blog.csdn.net/m0_65152767?spm=1010.2135.3001.5343");
        //3、使用 HttpClient 发起请求,获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4、解析响应,获取数据
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = response.getEntity();
            String content = EntityUtils.toString(httpEntity, "UTF-8");
            System.out.println(content);
        }
    }
}
相关推荐
中犇科技3 分钟前
郑州无代码APP开发公司哪家好呢?推荐
开发语言
周末也要写八哥6 分钟前
代码中的注释的重要性(二)
开发语言·python
殷紫川10 分钟前
90% Java 开发都踩过坑的 @Resource 与 @Autowired
java
kybs199121 分钟前
springboot租车系统--附源码68701
java·hadoop·spring boot·python·django·asp.net·php
过期动态1 小时前
MySQL中的约束
android·java·数据库·spring boot·mysql
wxin_VXbishe1 小时前
springboot新能源车充电站管理系统小程序-计算机毕业设计源码29213
java·c++·spring boot·python·spring·django·php
程序员陆通1 小时前
月烧 400 刀到不到 20 刀:我是怎么把 OpenClaw 的 Token 账单砍掉 95% 的
java·前端·数据库
江南十四行1 小时前
Python生成器与协程:从迭代器到异步编程的进阶之路
开发语言·python
代码漫谈2 小时前
一文学习 SpringBoot 的 application.yml 配置,基于 Spring Boot 3.2.x
java·spring boot·spring·配置文件
SamDeepThinking2 小时前
程序员如何接受工作内容毫无意义?
java·后端·程序员