五、案例
案例一:爬取天气数据
在这个案例中,我们将使用Java Spring Boot框架和Jsoup库来爬取天气数据。我们可以从指定的天气网站中获取实时的天气信息,并将其显示在我们的应用程序中。
- 创建一个新的Spring Boot应用程序,并添加所需的依赖库。
- 创建一个Controller类,在其中编写一个方法用于爬取天气数据。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/weather")
public class WeatherController {
@GetMapping("/forecast")
public String getWeatherForecast() {
try {
String url = "http://example.com/weather"; // 要爬取的天气网站URL
Document document = Jsoup.connect(url).get();
Elements forecasts = document.select(".forecast-item"); // 获取天气预报的元素
StringBuilder result = new StringBuilder();
for (Element forecast : forecasts) {
String date = forecast.select(".date").text(); // 获取日期
String weather = forecast.select(".weather").text(); // 获取天气情况
String temperature = forecast.select(".temperature").text(); // 获取温度
result.append(date).append(": ").append(weather).append(", ").append(temperature).append("\n");
}
return result.toString();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
- 在应用程序的主类中启动Spring Boot应用程序。
- 运行应用程序,并在浏览器中访问
http://localhost:8080/weather/forecast
,即可获取到天气预报信息。
案例二:爬取新闻头条
在这个案例中,我们将使用Java Spring Boot框架和Jsoup库来爬取新闻头条。我们可以从指定的新闻网站中获取最新的新闻标题和链接,并将其显示在我们的应用程序中。
- 创建一个新的Spring Boot应用程序,并添加所需的依赖库。
- 创建一个Controller类,在其中编写一个方法用于爬取新闻头条。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/news")
public class NewsController {
@GetMapping("/headlines")
public String getNewsHeadlines() {
try {
String url = "http://example.com/news"; // 要爬取的新闻网站URL
Document document = Jsoup.connect(url).get();
Elements headlines = document.select(".headline"); // 获取新闻标题的元素
StringBuilder result = new StringBuilder();
for (Element headline : headlines) {
String title = headline.text(); // 获取新闻标题
String link = headline.attr("href"); // 获取新闻链接
result.append(title).append(": ").append(link).append("\n");
}
return result.toString();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
- 在应用程序的主类中启动Spring Boot应用程序。
- 运行应用程序,并在浏览器中访问
http://localhost:8080/news/headlines
,即可获取到新闻头条信息。
案例三:爬取电影排行榜
在这个案例中,我们将使用Java Spring Boot框架和Jsoup库来爬取电影排行榜。我们可以从指定的电影网站中获取最新的电影排名、评分和简介,并将其显示在我们的应用程序中。
- 创建一个新的Spring Boot应用程序,并添加所需的依赖库。
- 创建一个Controller类,在其中编写一个方法用于爬取电影排行榜。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/movies")
public class MovieController {
@GetMapping("/top")
public String getTopMovies() {
try {
String url = "http://example.com/movies"; // 要爬取的电影网站URL
Document document = Jsoup.connect(url).get();
Elements movies = document.select(".movie"); // 获取电影排行榜的元素
StringBuilder result = new StringBuilder();
for (Element movie : movies) {
String rank = movie.select(".rank").text(); // 获取排名
String title = movie.select(".title").text(); // 获取电影标题
String rating = movie.select(".rating").text(); // 获取评分
String description = movie.select(".description").text(); // 获取简介
result.append(rank).append(". ").append(title).append(", Rating: ").append(rating).append("\n")
.append("Description: ").append(description).append("\n\n");
}
return result.toString();
} catch (Exception e) {
return "Error: " + e.getMessage();
}
}
}
- 在应用程序的主类中启动Spring Boot应用程序。
- 运行应用程序,并在浏览器中访问
http://localhost:8080/movies/top
,即可获取到电影排行榜信息。
这些案例只是展示了使用Java Spring Boot和Jsoup库进行爬虫开发的基本原理和方法。根据实际需求,我们可以根据网站的HTML结构和数据格式进行进一步的解析和处理。
最后