编写代码前,先分析天猫商品详情的地址,例如
这个地址简化后为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