5 changed files with 217 additions and 0 deletions
@ -0,0 +1,71 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
import com.example.bean.Movie; |
||||
|
import com.example.crawler.MovieCrawler; |
||||
|
import com.example.chart.DrawChart; |
||||
|
import java.io.FileWriter; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
// 1. 爬取数据
|
||||
|
MovieCrawler crawler = new MovieCrawler(); |
||||
|
List<Movie> movies = crawler.start(); |
||||
|
|
||||
|
// 2. 数据清洗
|
||||
|
List<Movie> cleanData = new ArrayList<>(); |
||||
|
for (Movie movie : movies) { |
||||
|
if (movie.getTitle() != null && !movie.getTitle().isBlank()) { |
||||
|
cleanData.add(movie); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// ===================== CSV 保存功能 恢复!=====================
|
||||
|
try (FileWriter writer = new FileWriter("movie_top100.csv")) { |
||||
|
writer.write("电影名称,评分,导演,上映年份\n"); |
||||
|
for (Movie m : cleanData) { |
||||
|
writer.write(m.getTitle() + "," + m.getScore() + "," + m.getDirector() + "," + m.getYear() + "\n"); |
||||
|
} |
||||
|
System.out.println("✅ CSV 文件已保存:movie_top100.csv"); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
// ==============================================================
|
||||
|
|
||||
|
// 3. 评分统计
|
||||
|
Map<String, Long> scoreCount = cleanData.stream() |
||||
|
.collect(Collectors.groupingBy(Movie::getScore, Collectors.counting())); |
||||
|
|
||||
|
// 4. 控制台打印表格
|
||||
|
System.out.println("\n=========================================="); |
||||
|
System.out.println(" 电影评分统计表格 "); |
||||
|
System.out.println("=========================================="); |
||||
|
System.out.printf("%-10s %-10s %-10s%n", "评分", "数量(部)", "占比(%)"); |
||||
|
System.out.println("------------------------------------------"); |
||||
|
|
||||
|
double total = cleanData.size(); |
||||
|
for (Map.Entry<String, Long> entry : scoreCount.entrySet()) { |
||||
|
String score = entry.getKey(); |
||||
|
long count = entry.getValue(); |
||||
|
double rate = (count * 100.0) / total; |
||||
|
System.out.printf("%-12s %-12d %-10.2f%n", score, count, rate); |
||||
|
} |
||||
|
|
||||
|
System.out.println("------------------------------------------"); |
||||
|
System.out.printf("总计:%d 部电影%n", (long) total); |
||||
|
System.out.println("=========================================="); |
||||
|
|
||||
|
// 5. 生成正常折线图:按排名 1→100 顺序连线
|
||||
|
List<Double> scoreList = new ArrayList<>(); |
||||
|
for (Movie movie : cleanData) { |
||||
|
scoreList.add(Double.parseDouble(movie.getScore())); |
||||
|
} |
||||
|
|
||||
|
DrawChart drawing = new DrawChart(); |
||||
|
drawing.drawByOrder(scoreList); |
||||
|
} |
||||
|
} |
||||
|
@ -0,0 +1,45 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.crawler</groupId> |
||||
|
<artifactId>movie-crawler</artifactId> |
||||
|
<version>1.0</version> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<!-- Jsoup:网页爬虫+HTML解析 --> |
||||
|
<dependency> |
||||
|
<groupId>org.jsoup</groupId> |
||||
|
<artifactId>jsoup</artifactId> |
||||
|
<version>1.15.4</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- XChart:生成图表(简单易用) --> |
||||
|
<dependency> |
||||
|
<groupId>org.knowm.xchart</groupId> |
||||
|
<artifactId>xchart</artifactId> |
||||
|
<version>3.8.4</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- OpenCSV:读写CSV文件 --> |
||||
|
<dependency> |
||||
|
<groupId>com.opencsv</groupId> |
||||
|
<artifactId>opencsv</artifactId> |
||||
|
<version>5.6</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- MySQL驱动(可选,存数据库) --> |
||||
|
<dependency> |
||||
|
<groupId>mysql</groupId> |
||||
|
<artifactId>mysql-connector-java</artifactId> |
||||
|
<version>8.0.33</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
</project> |
||||
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 25 KiB |
Loading…
Reference in new issue