如何使用 Java 构建简单爬虫?创建 Maven 项目并添加依赖项。编写爬虫逻辑,包括发送 HTTP 请求、解析 HTML 文档、提取链接并递归爬取网页。限制并发请求数,使用 Jsoup 库解析 HTML 文档,并使用 CompletableFuture 在异步模式下爬取网页。

Java 简单爬虫教程
如何使用 Java 构建爬虫
Java 是构建网络爬虫的理想语言,因为它提供了强大的库、良好的并发性和可扩展性。在本教程中,我们将介绍使用 Java 构建简单爬虫的基础知识。
先决条件
立即学习“Java免费学习笔记(深入)”;
依赖项
步骤 1:创建 Maven 项目
<code class="xml"><groupId>com.example</groupId>
<artifactId>crawler</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies></code>步骤 2:编写爬虫逻辑
在 Crawler.java 类中编写以下逻辑:
JEEWX-API 是一款JAVA版的微信开发SDK,支持微信公众号、小程序、微信企业号、支付宝生活号SDK和微博SDK。你可以基于她,快速的傻瓜化的进行微信开发、支付窗和微博开发。 基于jeewx-api开发,可以立即拥有简单易用的API,让开发更加轻松自如,节省更多时间。
0
<code class="java">import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Crawler {
public static void main(String[] args) throws IOException, InterruptedException {
// 创建 HTTP 客户端
HttpClient client = HttpClient.newHttpClient();
// 起始 URL
String url = "https://example.com";
// 限制最大并发请求数
int maxConcurrentRequests = 10;
// 创建一个异步请求列表
List<CompletableFuture<Void>> requests = List.of();
// 递归爬取网页
crawlRecursively(client, url, maxConcurrentRequests, requests);
// 等待所有请求完成
CompletableFuture.allOf(requests).get();
}
private static void crawlRecursively(HttpClient client, String url, int maxConcurrentRequests, List<CompletableFuture<Void>> requests)
throws IOException, InterruptedException {
// 发送 GET 请求
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 解析 HTML 文档
Document doc = Jsoup.parse(response.body());
// 提取页面上的链接
for (Element link : doc.select("a[href]")) {
String nextUrl = link.attr("href");
// 过滤不必要的链接
if (!nextUrl.startsWith("http") || nextUrl.startsWith("javascript")) {
continue;
}
// 限制并发请求数
if (requests.size() >= maxConcurrentRequests) {
CompletableFuture.allOf(requests).get();
requests.clear();
}
// 异步爬取新页面
requests.add(CompletableFuture.runAsync(() -> {
try {
crawlRecursively(client, nextUrl, maxConcurrentRequests, requests);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}));
}
}
}</code>步骤 3:运行爬虫
在命令行中执行以下命令:
<code class="bash">mvn clean install java -jar target/crawler-1.0-SNAPSHOT.jar</code>
如何限制并发请求数
通过设置 maxConcurrentRequests 变量限制并发请求数。这有助于避免服务器过载。
如何解析 HTML 文档
使用 Jsoup 库解析 HTML 文档。它提供了便捷的方法来提取页面上的元素和链接。
如何在异步模式下爬取网页
使用 CompletableFuture 在异步模式下爬取网页。这允许并行爬取多个页面,提高效率。
以上就是java简单爬虫教程的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号