URL:https://i.wap.58.com/wapimg/?page=1&infoid=25150533913132&cate=zufang&citylistname=bj
頁面包含「上一張圖」「下一張圖」鏈接,`page` 參數遞增/遞減。
CloseableHttpClient client = HttpClients.createDefault();
String baseUrl = "https://i.wap.58.com/wapimg/?page=1&infoid=25150533913132&cate=zufang&citylistname=bj";
String url = baseUrl;
while (url != null) {
HttpGet get = new HttpGet(url);
get.setHeader("User-Agent", "Mozilla/5.0");
try (CloseableHttpResponse resp = client.execute(get)) {
String html = EntityUtils.toString(resp.getEntity(), "utf-8");
Document doc = Jsoup.parse(html);
// 1. 下載圖片
Element img = doc.select("img[src]").first();
if (img != null) {
String imgUrl = img.attr("abs:src");
downloadImage(client, imgUrl); // 另寫方法保存文件
}
// 2. 找下一頁
Element next = doc.select("a:contains(下一張)").first();
url = (next != null) ? next.absUrl("href") : null;
Thread.sleep(500); // 禮貌延遲
}
}
分頁抓取核心是「解析列表頁 → 提取詳情/資源鏈接 → 尋找下一頁鏈接 → 循環」。HttpClient + Jsoup 是 Java 端經典組合。