一、建立基础的Maven工程
建立Maven工程点击next建立相应的工程名称
二、工程建立完成相应的项目目录
建立的工程.jpg三、添加相应的Maven依赖包
搜索相应Maven依赖网站 https://mvnrepository.com/
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
</dependencies>
依赖库添加.jpg
四、建立Java文件
public static void main(String[] args) throws IOException {
// 打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
// 发起Get请求
HttpGet httpGet = new HttpGet("https://www.jianshu.com/");
// 获取相应的相应
CloseableHttpResponse response = httpClient.execute(httpGet);
// 判断状态码
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity(); // 获取响应实体
String content = EntityUtils.toString(httpEntity, "utf8");
System.out.println("输出相应的结果:" + content);
}
}
爬虫简单代码.jpg
网友评论