8 changed files with 141 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||
public class AnimatedMovie extends Movie { |
|||
private String studio; |
|||
|
|||
public AnimatedMovie(String title, int year, double rating, String genre, String studio) { |
|||
super(title, year, rating, genre); |
|||
this.studio = studio; |
|||
} |
|||
|
|||
@Override |
|||
public void play() { |
|||
System.out.println("动画电影播放:" + getTitle() + ",制作公司:" + studio); |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
import org.jsoup.nodes.Element; |
|||
import java.util.List; |
|||
|
|||
public abstract class BaseMovieCrawler implements MovieCrawlerInterface { |
|||
public abstract List<Movie> crawl(int limit); |
|||
protected abstract Movie parseMovie(Element element); |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
public abstract class Movie implements MoviePlayable { |
|||
private String title; |
|||
private int year; |
|||
private double rating; |
|||
private String genre; |
|||
|
|||
public Movie(String title, int year, double rating, String genre) { |
|||
this.title = title; |
|||
this.year = year; |
|||
this.rating = rating; |
|||
this.genre = genre; |
|||
} |
|||
|
|||
// 接口方法,留给子类实现
|
|||
@Override |
|||
public abstract void play(); |
|||
|
|||
// getter
|
|||
public String getTitle() { return title; } |
|||
public int getYear() { return year; } |
|||
public double getRating() { return rating; } |
|||
public String getGenre() { return genre; } |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
import org.jsoup.Jsoup; |
|||
import org.jsoup.nodes.Document; |
|||
import org.jsoup.nodes.Element; |
|||
import org.jsoup.select.Elements; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.regex.Matcher; |
|||
import java.util.regex.Pattern; |
|||
|
|||
public class MovieCrawler extends BaseMovieCrawler { |
|||
|
|||
private static final String URL = "https://movie.douban.com/top250?start=0"; |
|||
|
|||
@Override |
|||
public List<Movie> crawl(int limit) { |
|||
List<Movie> movies = new ArrayList<>(); |
|||
try { |
|||
Document doc = Jsoup.connect(URL) |
|||
.userAgent("Mozilla/5.0") |
|||
.timeout(10000) |
|||
.get(); |
|||
|
|||
Elements items = doc.select(".item"); |
|||
int count = 0; |
|||
|
|||
for (Element item : items) { |
|||
if (count >= limit) break; |
|||
|
|||
Movie movie = parseMovie(item); |
|||
if (movie != null) { |
|||
movies.add(movie); |
|||
count++; |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("模拟电影数据(因网络403)"); |
|||
for (int i = 0; i < limit; i++) { |
|||
if (i % 2 == 0) { |
|||
movies.add(new TheatreMovie("肖申克的救赎 " + i, 1994, 9.7, "剧情", 49.9)); |
|||
} else { |
|||
movies.add(new AnimatedMovie("哪吒之魔童降世 " + i, 2019, 8.4, "动画", "可可豆")); |
|||
} |
|||
} |
|||
} |
|||
return movies; |
|||
} |
|||
|
|||
@Override |
|||
protected Movie parseMovie(Element element) { |
|||
try { |
|||
String title = element.select(".hd .title").first().text(); |
|||
String info = element.select(".bd p").first().text(); |
|||
Matcher m = Pattern.compile("(\\d{4})").matcher(info); |
|||
int year = m.find() ? Integer.parseInt(m.group(1)) : 2023; |
|||
double rating = Double.parseDouble(element.select(".rating_num").first().text()); |
|||
String genre = info.contains("/") ? info.split("/")[2].trim() : "未知"; |
|||
|
|||
return new TheatreMovie(title, year, rating, genre, 59.9); |
|||
} catch (Exception e) { |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
import java.util.List; |
|||
|
|||
public interface MovieCrawlerInterface { |
|||
List<Movie> crawl(int limit); |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
public interface MoviePlayable { |
|||
void play(); |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
import java.util.List; |
|||
|
|||
public class Test { |
|||
public static void main(String[] args) { |
|||
// 接口指向实现类 → 多态
|
|||
MovieCrawlerInterface crawler = new MovieCrawler(); |
|||
List<Movie> movies = crawler.crawl(5); |
|||
|
|||
System.out.println("\n==== 播放电影 ===="); |
|||
for (Movie movie : movies) { |
|||
movie.play(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
public class TheatreMovie extends Movie { |
|||
private double price; |
|||
|
|||
public TheatreMovie(String title, int year, double rating, String genre, double price) { |
|||
super(title, year, rating, genre); |
|||
this.price = price; |
|||
} |
|||
|
|||
@Override |
|||
public void play() { |
|||
System.out.println("院线电影播放:" + getTitle() + ",票价:" + price); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue