宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見(jiàn)問(wèn)題
產(chǎn)品動(dòng)態(tài)
精選推薦

從0到1:用Java爬蟲(chóng)優(yōu)雅地獲取京東商品詳情(附完整代碼)

管理 管理 編輯 刪除

在電商數(shù)據(jù)驅(qū)動(dòng)的時(shí)代,商品詳情數(shù)據(jù)成為市場(chǎng)分析、價(jià)格監(jiān)控、競(jìng)品調(diào)研的核心燃料。京東作為國(guó)內(nèi)頭部電商平臺(tái),其商品信息豐富、更新頻繁,是數(shù)據(jù)開(kāi)發(fā)者眼中的“香餑餑”。

本文將帶你從0到1,用Java語(yǔ)言實(shí)現(xiàn)一個(gè)可運(yùn)行的京東商品爬蟲(chóng),不僅能抓取商品標(biāo)題、價(jià)格、圖片、評(píng)價(jià)數(shù),還能應(yīng)對(duì)常見(jiàn)的反爬策略。全文附完整代碼,復(fù)制粘貼即可運(yùn)行。



一、為什么選擇Java做爬蟲(chóng)?

雖然Python是爬蟲(chóng)界的“老大哥”,但Java在企業(yè)級(jí)開(kāi)發(fā)中依舊占據(jù)主流,具備以下優(yōu)勢(shì):

  • 并發(fā)處理強(qiáng):利用多線程、線程池、異步IO,可輕松實(shí)現(xiàn)高并發(fā)抓??;
  • 生態(tài)成熟:Jsoup、HttpClient、Jackson等庫(kù)功能強(qiáng)大;
  • 部署方便:可直接打包為JAR,運(yùn)行在服務(wù)器或容器中;
  • 與業(yè)務(wù)系統(tǒng)無(wú)縫集成:采集后可直接入庫(kù)、入ES、入Kafka,打通數(shù)據(jù)閉環(huán)。


二、目標(biāo)明確:我們要抓什么?

以京東商品詳情頁(yè)為例,我們要抓取以下字段:

字段名示例值
商品標(biāo)題Apple iPhone 15 128G 藍(lán)色
商品價(jià)格5999.00
商品圖片https://img10.360buyimg.com/...
店鋪名稱(chēng)京東自營(yíng)旗艦店
評(píng)價(jià)數(shù)量50000+


三、技術(shù)選型


模塊技術(shù)方案
頁(yè)面下載Jsoup / HttpClient
HTML解析Jsoup CSS選擇器
JSON解析Jackson / Fastjson
代理與限速動(dòng)態(tài)代理池 + 隨機(jī)延時(shí)
反爬策略隨機(jī)UA、Referer、IP切換


四、環(huán)境準(zhǔn)備

1. 創(chuàng)建Maven項(xiàng)目

bash

mvn archetype:generate -DgroupId=com.jd.crawler -DartifactId=jd-crawler

2. 引入依賴(lài)(pom.xml

xml

<dependencies>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.17.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson2</artifactId>
        <version>2.0.42</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.14</version>
    </dependency>
</dependencies>


五、核心代碼實(shí)現(xiàn)

1. 下載商品詳情頁(yè)

java

public class HttpUtils {
    public static String getHtml(String url) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
        request.setHeader("Referer", "https://search.jd.com/");
        CloseableHttpResponse response = client.execute(request);
        return EntityUtils.toString(response.getEntity());
    }
}


2. 解析商品詳情頁(yè)

java

public class JDProductParser {
    public static Product parse(String skuId) throws IOException {
        String url = "https://item.jd.com/" + skuId + ".html";
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0")
                .header("Referer", "https://search.jd.com/")
                .get();

        String title = doc.select("div.sku-name").text().trim();
        String shop = doc.select("div.J-hove-wrap .name").text();
        String img = doc.select("img#spec-img").attr("src");
        if (!img.startsWith("http")) img = "https:" + img;

        // 獲取價(jià)格(異步接口)
        String priceUrl = "https://p.3.cn/prices/mgets?skuIds=J_" + skuId;
        String priceJson = HttpUtils.getHtml(priceUrl);
        String price = JSON.parseArray(priceJson).getJSONObject(0).getString("p");

        // 獲取評(píng)價(jià)數(shù)
        String commentUrl = "https://club.jd.com/comment/productCommentSummaries.action?referenceIds=" + skuId;
        String commentJson = HttpUtils.getHtml(commentUrl);
        int commentCount = JSON.parseObject(commentJson)
                .getJSONArray("CommentsCount")
                .getJSONObject(0)
                .getIntValue("CommentCount");

        return new Product(skuId, title, price, img, shop, commentCount);
    }
}


3. 商品實(shí)體類(lèi)

java

@Data
@AllArgsConstructor
public class Product {
    private String skuId;
    private String title;
    private String price;
    private String image;
    private String shop;
    private int commentCount;
}


4. 啟動(dòng)類(lèi)

java

public class CrawlerApp {
    public static void main(String[] args) throws IOException {
        String skuId = "100035288046"; // iPhone 15
        Product product = JDProductParser.parse(skuId);
        System.out.println(JSON.toJSONString(product, true));
    }
}


六、運(yùn)行效果(控制臺(tái)輸出)

JSON

{
  "skuId": "100035288046",
  "title": "Apple iPhone 15 128G 藍(lán)色",
  "price": "5999.00",
  "image": "https://img10.360buyimg.com/n1/s450x450_jfs/t1/123456.jpg",
  "shop": "京東自營(yíng)旗艦店",
  "commentCount": 50000
}


七、反爬策略與優(yōu)化建議


問(wèn)題解決方案
IP被封使用代理池(如阿布云、站大爺)
UA被識(shí)別隨機(jī)User-Agent池
滑塊驗(yàn)證碼使用打碼平臺(tái)或人工介入
接口限流控制頻率,隨機(jī)延時(shí)(1~3秒)
簽名接口分析JS加密邏輯,或調(diào)用官方API


八、進(jìn)階玩法(等你來(lái)挑戰(zhàn))

  • ? 多線程抓取 + 隊(duì)列調(diào)度(支持百萬(wàn)SKU)
  • ? 接入Spring Boot + MyBatis,實(shí)時(shí)入庫(kù)
  • ? 接入Elasticsearch,實(shí)現(xiàn)商品搜索
  • ? 接入Kafka,實(shí)時(shí)流式處理
  • ? 可視化展示:Spring Boot + ECharts


九、合法合規(guī)提醒

  • ? 禁止抓取用戶(hù)隱私信息(如收貨地址、手機(jī)號(hào))
  • ? 禁止高并發(fā)攻擊京東服務(wù)器
  • ? 對(duì)外商用需獲得京東授權(quán)
  • ? 建議優(yōu)先使用官方API(open.jd.com)


十、結(jié)語(yǔ)

本文從環(huán)境搭建、代碼實(shí)現(xiàn)、反爬策略到進(jìn)階方向,系統(tǒng)講解了如何用Java爬蟲(chóng)獲取京東商品詳情。希望你不僅能跑通代碼,更能在此基礎(chǔ)上構(gòu)建自己的數(shù)據(jù)采集系統(tǒng)。

如果你還想看:
  • “如何用Java爬取京東評(píng)論并做情感分析”
  • “如何用Java爬取京東秒殺庫(kù)存”
  • “如何用Spring Boot構(gòu)建爬蟲(chóng)調(diào)度平臺(tái)”
  • 歡迎留言告訴我,我們下期繼續(xù)!

點(diǎn)贊 + 收藏 + 轉(zhuǎn)發(fā),你的支持是我持續(xù)輸出的最大動(dòng)力!

請(qǐng)登錄后查看

one-Jason 最后編輯于2025-09-23 16:19:34

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點(diǎn)贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無(wú)簡(jiǎn)介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
102
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見(jiàn)問(wèn)題 產(chǎn)品動(dòng)態(tài) 精選推薦 首頁(yè)頭條 首頁(yè)動(dòng)態(tài) 首頁(yè)推薦
取 消 確 定
回復(fù)
回復(fù)
問(wèn)題:
問(wèn)題自動(dòng)獲取的帖子內(nèi)容,不準(zhǔn)確時(shí)需要手動(dòng)修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請(qǐng)輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊(cè)

切換手機(jī)號(hào)登錄

{{ bind_phone ? '綁定手機(jī)' : '手機(jī)登錄'}}

{{codeText}}
切換微信登錄/注冊(cè)
暫不綁定
CRMEB客服

CRMEB咨詢(xún)熱線 咨詢(xún)熱線

400-8888-794

微信掃碼咨詢(xún)

CRMEB開(kāi)源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服