首页 > 大数据 > 数据分析 > 京东获取关键词商品列表API接口实战案例
京东开放平台提供的
jd.item_search接口,允许开发者通过关键词搜索商品,并返回商品列表。该接口支持多种筛选条件,如价格范围、类目ID、排序方式等,适用于商品推荐、市场分析等场景。
AppKey和
AppSecret,用于后续接口调用。jd.item_search接口文档,详细阅读请求参数、返回格式及调用限制。AppSecret使用MD5或HMAC-SHA256算法生成签名,确保请求合法性。requests库)发送POST请求到京东开放平台的API接口。Authorization(如OAuth2.0的
Bearer Token)、
Content-Type(如
application/json)等信息。AppKey、
timestamp(时间戳)、
sign(签名)等。pythonimport requestsimport hashlibimport timeimport json# 配置参数app_key = 'YOUR_APP_KEY' # 替换为实际AppKeyapp_secret = 'YOUR_APP_SECRET' # 替换为实际AppSecretkeyword = 'iPhone 13' # 搜索关键词# 生成签名def generate_sign(params, app_secret):sorted_params = sorted(params.items())query_string = ''.join(f'{k}{v}' for k, v in sorted_params)sign = hashlib.md5((query_string + app_secret).encode()).hexdigest().upper()return sign# 发送请求def search_items(keyword, app_key, app_secret):url = 'https://api.jd.com/routerjson'timestamp = int(time.time() * 1000)params = {'method': 'jd.item_search','app_key': app_key,'timestamp': timestamp,'v': '2.0','sign_method': 'md5','param_json': json.dumps({'keyword': keyword,'page': 1,'page_size': 10,'sort': 'sales','order': 'desc'})}params['sign'] = generate_sign(params, app_secret)response = requests.post(url, data=params)return response.json()# 主函数def main():result = search_items(keyword, app_key, app_secret)if result.get('code') == '0':items = result.get('data', {}).get('item_list', [])for item in items:print(f"商品名称: {item.get('name')}")print(f"商品价格: {item.get('price')}")print(f"商品图片: {item.get('image')}")print('-' * 50)else:print(f"请求失败,错误码:{result.get('code')},错误信息:{result.get('errmsg')}")if __name__ == "__main__":main()
AppKey和
AppSecret,避免泄露。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70046834/viewspace-3088670/ ,如需转载,请注明出处,否则将追究法律责任。