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.

45 lines
1.3 KiB

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;
public class XiaohongshuStrategy implements CrawlStrategy {
private static final String URL = "https://www.xiaohongshu.com";
@Override
public List<Movie> crawl(int limit) {
List<Movie> list = new ArrayList<>();
try {
Document doc = Jsoup.connect(URL)
.userAgent("Mozilla/5.0")
.timeout(10000)
.get();
Elements items = doc.select("div");
int count = 0;
for (Element e : items) {
if (count >= limit) break;
Movie m = parseMovie(e);
if (m != null) {
list.add(m);
count++;
}
}
} catch (Exception ex) {
System.out.println("小红书抓取失败(反爬保护),已跳过");
}
return list;
}
@Override
public Movie parseMovie(Element element) {
try {
String title = element.select("h2").first().text();
return new XiaohongshuMovie(title, 2024, 9.2, "笔记", "小红书用户");
} catch (Exception e) {
return null;
}
}
}