淘宝批量发货发布工具, 淘宝批量上传商品软件, 淘宝批量上架软件【python】

简介: 使用Selenium实现自动化操作淘宝卖家后台支持三种核心功能

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

代码功能说明:
使用Selenium实现自动化操作淘宝卖家后台
支持三种核心功能:批量上传商品、批量上架商品和批量发货
商品信息通过Excel文件导入,支持多商品批量处理
包含完善的异常处理和等待机制,确保操作稳定性
采用面向对象设计,代码结构清晰易于扩展
模拟真实用户操作流程,避免被平台检测为机器人

import os
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException

class TaobaoBatchTool:
def init(self, username, password):
self.username = username
self.password = password
self.driver = None
self.wait_time = 10
self.init_driver()

def init_driver(self):
    """初始化浏览器驱动"""
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument('--start-maximized')
    self.driver = webdriver.Chrome(options=options)
    self.driver.implicitly_wait(self.wait_time)

def login(self):
    """登录淘宝卖家中心"""
    try:
        self.driver.get('https://login.taobao.com/')
        WebDriverWait(self.driver, self.wait_time).until(
            EC.presence_of_element_located((By.ID, 'fm-login-id'))
        ).send_keys(self.username)

        self.driver.find_element(By.ID, 'fm-login-password').send_keys(self.password)
        self.driver.find_element(By.CSS_SELECTOR, '.fm-button.fm-submit.password-login').click()

        # 等待登录成功
        WebDriverWait(self.driver, self.wait_time).until(
            EC.presence_of_element_located((By.LINK_TEXT, '卖家中心'))
        )
        print("登录成功")
        return True
    except Exception as e:
        print(f"登录失败: {str(e)}")
        return False

def batch_upload_products(self, product_file):
    """批量上传商品"""
    try:
        products = pd.read_excel(product_file)
        self.driver.get('https://sell.taobao.com/auction/merchandise/auction_list.htm')

        for index, row in products.iterrows():
            try:
                # 点击发布商品按钮
                WebDriverWait(self.driver, self.wait_time).until(
                    EC.element_to_be_clickable((By.LINK_TEXT, '发布商品'))
                ).click()

                # 选择商品类目
                WebDriverWait(self.driver, self.wait_time).until(
                    EC.presence_of_element_located((By.ID, 'J_Category'))
                ).click()

                # 填写商品信息
                self._fill_product_info(row)

                # 提交商品
                self.driver.find_element(By.ID, 'J_Submit').click()
                print(f"成功上传商品: {row['title']}")
                time.sleep(2)  # 防止操作过快

            except Exception as e:
                print(f"上传商品 {row['title']} 失败: {str(e)}")
                continue

        return True
    except Exception as e:
        print(f"批量上传商品失败: {str(e)}")
        return False

def _fill_product_info(self, product):
    """填写商品详细信息"""
    # 商品标题
    self.driver.find_element(By.ID, 'J_Title').send_keys(product['title'])

    # 商品价格
    self.driver.find_element(By.ID, 'J_Price').send_keys(str(product['price']))

    # 商品库存
    self.driver.find_element(By.ID, 'J_Quantity').send_keys(str(product['quantity']))

    # 商品描述
    self.driver.switch_to.frame('J_DescIframe')
    self.driver.find_element(By.TAG_NAME, 'body').send_keys(product['description'])
    self.driver.switch_to.default_content()

    # 上传主图
    for img in product['images'].split(','):
        self.driver.find_element(By.CSS_SELECTOR, '.uploader-pick').send_keys(os.path.abspath(img.strip()))
        time.sleep(1)

def batch_list_products(self, product_ids):
    """批量上架商品"""
    try:
        self.driver.get('https://sell.taobao.com/auction/merchandise/auction_list.htm')

        for product_id in product_ids:
            try:
                # 搜索商品
                search_box = WebDriverWait(self.driver, self.wait_time).until(
                    EC.presence_of_element_located((By.ID, 'search-key'))
                )
                search_box.clear()
                search_box.send_keys(product_id)
                search_box.send_keys(Keys.RETURN)

                # 勾选商品
                WebDriverWait(self.driver, self.wait_time).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, f'input[value="{product_id}"]'))
                ).click()

                # 点击上架按钮
                self.driver.find_element(By.LINK_TEXT, '上架').click()
                print(f"成功上架商品ID: {product_id}")
                time.sleep(1)

            except Exception as e:
                print(f"上架商品ID {product_id} 失败: {str(e)}")
                continue

        return True
    except Exception as e:
        print(f"批量上架商品失败: {str(e)}")
        return False

def batch_ship_orders(self, order_file):
    """批量发货"""
    try:
        orders = pd.read_excel(order_file)
        self.driver.get('https://trade.taobao.com/trade/itemlist/list_sold_items.htm')

        for index, row in orders.iterrows():
            try:
                # 搜索订单
                search_box = WebDriverWait(self.driver, self.wait_time).until(
                    EC.presence_of_element_located((By.ID, 'search-order-input'))
                )
                search_box.clear()
                search_box.send_keys(row['order_id'])
                search_box.send_keys(Keys.RETURN)

                # 勾选订单
                WebDriverWait(self.driver, self.wait_time).until(
                    EC.presence_of_element_located((By.CSS_SELECTOR, f'input[value="{row["order_id"]}"]'))
                ).click()

                # 点击发货按钮
                self.driver.find_element(By.LINK_TEXT, '发货').click()

                # 填写物流信息
                self._fill_shipping_info(row)

                # 确认发货
                self.driver.find_element(By.ID, 'J_Confirm').click()
                print(f"成功发货订单: {row['order_id']}")
                time.sleep(1)

            except Exception as e:
                print(f"发货订单 {row['order_id']} 失败: {str(e)}")
                continue

        return True
    except Exception as e:
        print(f"批量发货失败: {str(e)}")
        return False

def _fill_shipping_info(self, order):
    """填写物流信息"""
    # 选择物流公司
    self.driver.find_element(By.CSS_SELECTOR, '.logistics-company').click()
    self.driver.find_element(By.XPATH, f"//li[contains(text(), '{order['shipping_company']}')]").click()

    # 填写运单号
    self.driver.find_element(By.ID, 'J_LogisticCode').send_keys(order['tracking_number'])

def close(self):
    """关闭浏览器"""
    if self.driver:
        self.driver.quit()

def main():

# 示例用法
tool = TaobaoBatchTool('your_username', 'your_password')

try:
    if tool.login():
        # 批量上传商品
        tool.batch_upload_products('products.xlsx')

        # 批量上架商品
        product_ids = ['123456', '789012']
        tool.batch_list_products(product_ids)

        # 批量发货
        tool.batch_ship_orders('orders.xlsx')

finally:
    tool.close()

if name == 'main':
main()

相关文章
|
3月前
|
存储 缓存 测试技术
理解Python装饰器:简化代码的强大工具
理解Python装饰器:简化代码的强大工具
|
4月前
|
程序员 测试技术 开发者
Python装饰器:简化代码的强大工具
Python装饰器:简化代码的强大工具
237 92
|
3月前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
323 3
|
3月前
|
算法 安全 数据安全/隐私保护
Python随机数函数全解析:5个核心工具的实战指南
Python的random模块不仅包含基础的随机数生成函数,还提供了如randint()、choice()、shuffle()和sample()等实用工具,适用于游戏开发、密码学、统计模拟等多个领域。本文深入解析这些函数的用法、底层原理及最佳实践,帮助开发者高效利用随机数,提升代码质量与安全性。
850 0
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
323 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
349 104
|
3月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
281 103
|
3月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
208 82
|
2月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
242 3
|
2月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
495 3

推荐镜像

更多