You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

package com.example.moviecli.strategy;
import com.example.moviecli.model.Movie;
import com.example.moviecli.exception.ParseFailedException;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
public class DoubanBookStrategy implements MovieCrawlStrategy {
@Override
public boolean supports(String url) {
return url.contains("book.douban.com/top250");
}
@Override
public List<Movie> parse(Document doc) throws ParseFailedException {
try {
List<Movie> books = new ArrayList<>();
Elements items = doc.select(".item");
int rank = 1;
for (Element item : items) {
String title = item.select(".pl2 a").text().trim();
String score = item.select(".rating_nums").text();
books.add(new Movie(rank++, title, "", score, "图书", "豆瓣图书"));
}
return books;
} catch (Exception e) {
throw new ParseFailedException("豆瓣图书解析失败", e);
}
}
}