springboot 爬虫例子
时间: 2023-09-12 18:05:57 浏览: 50
以下是一个使用 Spring Boot 实现的简单爬虫例子:
1. 创建一个 Spring Boot 项目并添加 Web 和 Jsoup 依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
</dependencies>
```
2. 创建一个 Controller 类,定义一个 GET 请求方法:
```java
@RestController
public class CrawlerController {
@GetMapping("/crawl")
public String crawl() throws IOException {
// TODO: 实现爬虫逻辑
return "Crawling...";
}
}
```
3. 在 `crawl` 方法中实现爬虫逻辑,例如:
```java
@GetMapping("/crawl")
public String crawl() throws IOException {
Document doc = Jsoup.connect("https://www.baidu.com/").get();
String title = doc.title();
return "Title: " + title;
}
```
4. 运行项目并访问 `http://localhost:8080/crawl`,即可看到爬虫结果。
以上是一个简单的 Spring Boot 爬虫例子,你可以根据自己的需求进一步完善和优化。注意不要过度请求目标网站,以免造成不必要的麻烦。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)