淘宝批量上架软件脚本,电商一键上货软件, 淘宝一键铺货软件【python】

简介: 核心功能:实现淘宝商品批量上传,包含登录认证、商品数据处理、图片处理和API调用多线程处理

文章附件下载:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:7315

核心功能:实现淘宝商品批量上传,包含登录认证、商品数据处理、图片处理和API调用
多线程处理:使用ThreadPoolExecutor实现并发上传,提高效率
图片处理:自动缩放和转换商品图片,保持宽高比
数据读取:支持从Excel文件读取商品信息,包含标题、价格、库存等关键字段
错误处理:完善的异常捕获和错误提示机制
配置管理:通过配置文件管理API密钥等敏感信息
分类映射:内置常见商品分类ID映射表

import os
import time
import requests
import pandas as pd
from PIL import Image
from hashlib import md5
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor

class TaobaoUploader:
def init(self, config_file='config.ini'):
self.session = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Referer': 'https://www.taobao.com'
}
self.load_config(config_file)
self.login()

def load_config(self, config_file):
    """加载配置文件"""
    if not os.path.exists(config_file):
        raise FileNotFoundError("配置文件不存在")

    config = {}
    with open(config_file, 'r', encoding='utf-8') as f:
        for line in f:
            if '=' in line:
                key, value = line.strip().split('=', 1)
                config[key.strip()] = value.strip()

    self.api_url = config.get('api_url', '')
    self.app_key = config.get('app_key', '')
    self.app_secret = config.get('app_secret', '')
    self.session_key = config.get('session_key', '')
    self.max_workers = int(config.get('max_workers', '5'))
    self.image_dir = config.get('image_dir', 'images')

def login(self):
    """模拟登录获取token"""
    params = {
        'method': 'taobao.user.seller.get',
        'app_key': self.app_key,
        'session': self.session_key,
        'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
        'format': 'json',
        'v': '2.0',
        'sign_method': 'md5'
    }

    sign_str = self.app_secret + ''.join([f'{k}{v}' for k,v in sorted(params.items())]) + self.app_secret
    params['sign'] = md5(sign_str.encode()).hexdigest()

    try:
        resp = self.session.get(self.api_url, params=params, headers=self.headers)
        if resp.json().get('error_response'):
            raise Exception("登录失败: " + resp.json()['error_response']['msg'])
        print("登录成功")
    except Exception as e:
        print(f"登录异常: {str(e)}")
        raise

def process_image(self, image_path, target_size=(800, 800)):
    """处理商品图片"""
    if not os.path.exists(image_path):
        return None

    try:
        img = Image.open(image_path)
        if img.mode != 'RGB':
            img = img.convert('RGB')

        # 保持宽高比缩放
        img.thumbnail(target_size)

        # 生成新文件名
        new_name = f"processed_{os.path.basename(image_path)}"
        save_path = os.path.join('processed_images', new_name)
        os.makedirs('processed_images', exist_ok=True)
        img.save(save_path, quality=95)
        return save_path
    except Exception as e:
        print(f"图片处理失败: {str(e)}")
        return None

def read_product_data(self, excel_file):
    """读取商品数据Excel"""
    try:
        df = pd.read_excel(excel_file)
        required_cols = ['title', 'price', 'stock', 'description', 'category']
        if not all(col in df.columns for col in required_cols):
            raise ValueError("Excel缺少必要列")

        products = []
        for _, row in df.iterrows():
            product = {
                'title': str(row['title']),
                'price': float(row['price']),
                'stock': int(row['stock']),
                'desc': str(row['description']),
                'category': str(row['category']),
                'images': []
            }

            # 处理图片列
            if 'images' in df.columns:
                img_str = str(row['images'])
                if img_str:
                    product['images'] = [img.strip() for img in img_str.split(',')]

            products.append(product)

        return products
    except Exception as e:
        print(f"读取商品数据失败: {str(e)}")
        return []

def upload_product(self, product):
    """上传单个商品"""
    try:
        # 处理图片
        processed_images = []
        for img in product['images']:
            img_path = os.path.join(self.image_dir, img)
            processed_path = self.process_image(img_path)
            if processed_path:
                processed_images.append(processed_path)

        # 构造API参数
        params = {
            'method': 'taobao.item.add',
            'app_key': self.app_key,
            'session': self.session_key,
            'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            'format': 'json',
            'v': '2.0',
            'sign_method': 'md5',
            'title': product['title'],
            'price': product['price'],
            'num': product['stock'],
            'desc': product['desc'],
            'cid': self.get_category_id(product['category']),
            'image': ','.join(processed_images) if processed_images else ''
        }

        sign_str = self.app_secret + ''.join([f'{k}{v}' for k,v in sorted(params.items())]) + self.app_secret
        params['sign'] = md5(sign_str.encode()).hexdigest()

        # 调用API
        resp = self.session.post(self.api_url, data=params, headers=self.headers)
        result = resp.json()

        if 'error_response' in result:
            return False, result['error_response']['msg']
        else:
            return True, result['item_add_response']['item']['num_iid']

    except Exception as e:
        return False, str(e)

def get_category_id(self, category_name):
    """获取分类ID"""
    # 这里简化处理,实际应该调用淘宝API获取
    category_map = {
        '女装': '162104',
        '男装': '162103',
        '数码': '1512',
        '家电': '1512',
        '美妆': '1801'
    }
    return category_map.get(category_name, '162104')

def batch_upload(self, excel_file, max_workers=5):
    """批量上传商品"""
    products = self.read_product_data(excel_file)
    if not products:
        return False

    success_count = 0
    failed_items = []

    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        futures = []
        for product in products:
            futures.append(executor.submit(self.upload_product, product))

        for future in futures:
            success, result = future.result()
            if success:
                success_count += 1
                print(f"上传成功,商品ID: {result}")
            else:
                failed_items.append(result)
                print(f"上传失败: {result}")

    print(f"\n上传完成,成功: {success_count}, 失败: {len(failed_items)}")
    if failed_items:
        print("失败商品:")
        for item in failed_items:
            print(f"- {item}")

    return success_count > 0

if name == 'main':
try:
uploader = TaobaoUploader()
uploader.batch_upload('products.xlsx', max_workers=5)
except Exception as e:
print(f"程序异常: {str(e)}")

==2.31.0
pandas==2.1.0
Pillow==10.0.0
openpyxl==3.1.2

相关文章
|
2月前
|
存储 分布式计算 大数据
基于Python大数据的的电商用户行为分析系统
本系统基于Django、Scrapy与Hadoop技术,构建电商用户行为分析平台。通过爬取与处理海量用户数据,实现行为追踪、偏好分析与个性化推荐,助力企业提升营销精准度与用户体验,推动电商智能化发展。
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
3月前
|
JSON 缓存 开发者
淘宝商品详情接口(item_get)企业级全解析:参数配置、签名机制与 Python 代码实战
本文详解淘宝开放平台taobao.item_get接口对接全流程,涵盖参数配置、MD5签名生成、Python企业级代码实现及高频问题排查,提供可落地的实战方案,助你高效稳定获取商品数据。
|
3月前
|
JSON API 数据安全/隐私保护
Python采集淘宝评论API接口及JSON数据返回全流程指南
Python采集淘宝评论API接口及JSON数据返回全流程指南
缓存 监控 数据挖掘
79 0
|
3月前
|
Web App开发 监控 API
淘宝 item_review 接口深度分析及 Python 实现
淘宝item_review接口用于获取商品用户评价、评分、追评等数据,支持商品口碑分析、用户需求挖掘、竞品对比等场景,是电商数据分析的重要工具。
|
3月前
|
存储 监控 API
Python实战:跨平台电商数据聚合系统的技术实现
本文介绍如何通过标准化API调用协议,实现淘宝、京东、拼多多等电商平台的商品数据自动化采集、清洗与存储。内容涵盖技术架构设计、Python代码示例及高阶应用(如价格监控系统),提供可直接落地的技术方案,帮助开发者解决多平台数据同步难题。
|
2月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
233 3
|
2月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
489 3

推荐镜像

更多