当前位置: 首页 > news >正文

Java爬虫1688详情api接口实战解析

下面给出一份可直接落地的「Java 版 1688 商品详情 API 爬虫」完整示例,覆盖签名算法、HTTP 调用、JSON 解析、异常重试、频率控制等关键要点,复制即可运行。
(注:1688 接口需企业认证并申请 AppKey / AppSecret,以下代码以官方 REST 网关item_get为例,也可平替为第三方代理网关,只需换域名即可 。)


一、Maven 依赖

<dependencies> <!-- HTTP --> <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.3.1</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version> </dependency> </dependencies>

二、签名工具(1688 官方 MD5 签名规则)

public class SignUtil { public static String sign(TreeMap<String, String> params, String appSecret) { StringBuilder sb = new StringBuilder(appSecret); for (Map.Entry<String, String> e : params.entrySet()) { sb.append(e.getKey()).append(e.getValue()); } sb.append(appSecret); return md5(sb.toString()).toUpperCase(); } private static String md5(String raw) { try { byte[] bs = MessageDigest.getInstance("MD5").digest(raw.getBytes(StandardCharsets.UTF_8)); StringBuilder hex = new StringBuilder(); for (byte b : bs) hex.append(String.format("%02X", b & 0xFF)); return hex.toString(); } catch (Exception e) { throw new RuntimeException(e); } } }

三、统一入口 ——ItemGetService

public class ItemGetService { private static final String GATEWAY = "https://api.1688.com/router/rest"; private final String appKey; private final String appSecret; private final CloseableHttpClient http; public ItemGetService(String appKey, String appSecret) { this.appKey = appKey; this.appSecret = appSecret; this.http = HttpClients.custom() .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create() .setMaxConnTotal(50).setMaxConnPerRoute(10).build()) .build(); } /** 获取商品详情(自动重试 3 次) */ public ItemDO getItem(long numIid) throws IOException { TreeMap<String, String> params = new TreeMap<>(); params.put("method", "item_get"); params.put("app_key", appKey); params.put("timestamp", String.valueOf(System.currentTimeMillis())); params.put("num_iid", String.valueOf(numIid)); params.put("v", "2.0"); params.put("sign_method", "md5"); params.put("format", "json"); params.put("sign", SignUtil.sign(params, appSecret)); String url = GATEWAY + "?" + URLEncodedUtils.format( params.entrySet().stream() .map(e -> new BasicNameValuePair(e.getKey(), e.getValue())) .collect(Collectors.toList()), StandardCharsets.UTF_8); for (int i = 0; i < 3; i++) { try (CloseableHttpResponse resp = http.execute(new HttpGet(url))) { if (resp.getCode() == 200) { JsonNode root = new ObjectMapper().readTree(EntityUtils.toString(resp.getEntity())); if ("0".equals(root.get("code").asText())) { return new ObjectMapper().convertValue(root.get("item"), ItemDO.class); } throw new IllegalStateException("API 业务错误: " + root.get("msg").asText()); } } catch (Exception e) { if (i == 2) throw e; try { Thread.sleep(1000); } catch (InterruptedException ignore) {} } } throw new RuntimeException("重试 3 次仍失败"); } @Data // lombok public static class ItemDO { private String title; private BigDecimal price; private Integer num; // 库存 private String picUrl; private List<Sku> skus; } public void shutdown() throws IOException { http.close(); } }

四、频率控制 + 批量调用示例

public class CrawlerBoot { public static void main(String[] args) throws Exception { ItemGetService api = new ItemGetService("你的AppKey", "你的AppSecret"); List<Long> numIids = List.of(610947572360L, 623456789012L); // 商品ID池 for (Long id : numIids) { ItemGetService.ItemDO item = api.getItem(id); System.out.printf("标题=%s 价格=%s 库存=%d%n", item.getTitle(), item.getPrice(), item.getNum()); Thread.sleep(350); // 约 3 次/秒,低于官方 5 次/秒限制 } api.shutdown(); } }

五、常见坑 & 优化建议

  1. 签名顺序必须TreeMap升序,否则 4005 授权失败。

  2. 免费账号每日调用上限 1 万次,超出需购买套餐;峰值时段做好限流与重试。

  3. 如需 SKU 图、阶梯价、近 30 天销量,需在参数额外指定fields=skus,priceRange,saleInfo

  4. 若走第三方代理网关(如api-gw.onebound.cn),签名规则不变,仅换域名即可。

  5. 数据落库时建议用ON DUPLICATE KEY UPDATE做幂等,避免重复写入。


六、一句话总结
以上代码即为“Java 爬虫 1688 详情 API 接口”的最小可运行骨架,已帮你屏蔽掉签名、编码、重试、频率等所有细节,直接填上自己的 AppKey / Secret 即可把 1688 商品库变成本地数据表

http://www.gsyq.cn/news/122383.html

相关文章:

  • 001 PyTorch实战:手写数字识别(MNIST)从零开始
  • Ramile终极指南:5分钟搞定软件著作权代码提取
  • 5分钟搞定Python控制Android设备:py-scrcpy-client实战指南
  • 昂瑞微推出了面向移动电源行业的一站式智能方案-OM70201MV
  • AI 训练素材、数据集供应商推荐:图片视频数据选哪家? - 品牌2026
  • 打印等边三角形
  • 2025年12月矿物铸件的头部企业推荐榜:南通盟鼎新材料五星闪耀,减振赋能精度,这些企业领跑装备升级 - 海棠依旧大
  • 嵌入式音频调试实战指南:从问题定位到系统优化
  • 国内混合机品牌五大佼佼者出炉!揭秘这些混合设备工厂的硬核实力 - 速递信息
  • 2025 宝藏十大图库!自媒体、小红书、公众号配图必备 - 品牌2026
  • FireFox 配置
  • 2025年12月洁净无氧烘箱厂家新标杆:德菲瑞斯高真空无氧烘箱、HMDS 无氧烘箱、真空无氧烘箱、充氮无氧烘箱、高温无氧烘箱、HMDS真空无氧烘箱、精准控温更可靠 - 海棠依旧大
  • Row下的两个Text文本自适应宽度,并以中间为基准滚动
  • Vite:现代前端构建工具的革命与实战指南
  • 立体相机标定
  • 专业解读|北京刑事律师事务所实力排名:2025靠谱机构辩护方案对比 - 老周说教育
  • 2025年煤粉仓防爆门实力厂家权威推荐榜单:重力防爆门/矩形防爆门/弹簧防爆门源头厂家精选 - 品牌推荐官
  • 突破性能瓶颈:iStoreOS系统加速实战指南
  • 2025年BIPV防水支架源头厂家权威推荐榜单:屋面分布式BIPV光伏支架/BIPV屋顶防水光伏支架/BIPV建筑一体化光伏支架源头厂家精选 - 品牌推荐官
  • 半导体测试之静态测试参数有哪些?为什么要进行测试?
  • 实用参考!2025-2026北京律师事务所排行榜:全维度服务能力解析 - 老周说教育
  • 如何高效定制B站API认证凭证:全新Cookies配置指南
  • 减肥产品怎么选不踩雷?1000+用户真实反馈,2025 高口碑品牌无滤镜测评榜单 - 速递信息
  • 【前瞻预告】2025-2026北京律师事务所哪家好?预热榜单与核心机构解析 - 老周说教育
  • 微算法科技(NASDAQ MLGO)使用多线程技术提升区块链共识算法的性能
  • 上海建筑防水服务市场观察:五家服务商的技术特点与场景适配分析,上海防水补漏 TOP5 出炉 - shruisheng
  • Omnissa Unified Access Gateway 2512 - 远程安全的应用程序访问
  • 减肥产品真能安全见效?2025 高口碑品牌临床数据测评,减脂效率与安全性双验证 - 速递信息
  • 全网首先发现 android NSDManager做mDNS发现可能无反应
  • 从零开始构建DE25-Nano的Linux Image(LXDE)