用戶從百度文庫抓數據寫入日志,**不算完整爬蟲**。
RequestConfig config = RequestConfig.custom()
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
.setHeader("Referer", "https://www.target.com/page.html")
.setHeader("Accept", "image/webp,image/apng,image/*,*/*;q=0.8")
.build();
data-src、JS渲染、WebP格式 → 需Selenium/Playwright/Puppeteer無頭瀏覽器。// HttpClient + Jsoup 簡易版(靜態頁面)
public void crawlImages(String startUrl) {
Queue queue = new LinkedList<>();
Set visited = new HashSet<>();
queue.add(startUrl);
while (!queue.isEmpty()) {
String url = queue.poll();
if (visited.contains(url)) continue;
visited.add(url);
String html = HttpClientUtil.get(url); // 含Header模擬
Document doc = Jsoup.parse(html);
// 提取圖片
doc.select("img").forEach(img -> {
String src = img.attr("abs:src");
downloadImage(src);
});
// 提取鏈接入隊
doc.select("a[href]").forEach(a -> {
String href = a.attr("abs:href");
if (href.startsWith("https://target.com")) queue.add(href);
});
}
}