通过Java自动获取天猫或淘宝商品的主图

编写代码前,先分析天猫商品详情的地址,例如

https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.6.56bd529dWWPn2c&id=606321601988&skuId=4419477574301&areaId=410100&user_id=2201400309579&cat_id=2&is_b=1&rn=2aa607a61dfbfcf4f89aafbc56e09792

这个地址简化后为https://detail.tmall.com/item.htm?id=606321601988

 

606321601988是全网唯一的,就是咋们常说的auctionId。

 

通过Java自动获取天猫或淘宝商品的主图,直接上代码

/**
 * 获取主图
 */
public static String getDownloadImgURL(String auctionId) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpPost = new HttpGet("https://detail.tmall.com/item.htm?id="
            + auctionId);
    HttpResponse response = httpclient.execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {// 正常
        String result = EntityUtils.toString(response.getEntity());
        // 获取图片标签
        int startIndex = result.indexOf("J_ImgBooth");
        String str = result.substring(startIndex);
        str = str.substring(0, str.indexOf("/>"));
        // 获取图片URL
        startIndex = str.indexOf("src=\"");
        str = str.substring(startIndex + 5);
        str = str.substring(0, str.indexOf("\""));
        str = "https:" + str;
        return str;
    }
    return null;
}
public static void main(String[] args) throws Exception {
    System.out.println(getDownloadImgURL("606321601988"));
}

运行后的结果为:


https://img.alicdn.com/imgextra/https://img.alicdn.com/imgextra/i3/2201400309579/O1CN01X5DxZx2KdEVkAYbhV_!!2201400309579.jpg_430x430q90.jpg

代码详情请见 http://www.zrscsoft.com/sitepic/12106.html 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值