阿里巴巴私信群发工具,淘宝卖家私信发消息脚本,批量发送私信软件【python】

61 阅读2分钟

下载地址:www.pan38.com/dow/share.p… 提取密码:1928

这个工具使用了Selenium自动化测试框架来模拟浏览器操作,实现了淘宝/阿里巴巴的自动登录和私信发送功能。代码包含了随机延迟、异常处理等机制来避免被检测为自动化脚本。使用时需要准备消息模板和联系人列表的JSON文件,并填写正确的账号密码。

import time import random import json 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 TaobaoMassMessage: def init(self): self.driver = None self.logged_in = False self.message_templates = [] self.contact_list = [] self.config = { 'login_url': 'login.taobao.com/', 'message_url': 'msg.taobao.com/', 'wait_time': 10, 'delay_range': (3, 8) }

def init_driver(self):
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    self.driver = webdriver.Chrome(options=options)
    self.driver.maximize_window()
    
def login(self, username, password):
    if not self.driver:
        self.init_driver()
        
    self.driver.get(self.config['login_url'])
    
    try:
        # 切换到账号密码登录
        switch_btn = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login"]/div[1]/i'))
        )
        switch_btn.click()
        
        # 输入用户名
        username_input = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.ID, 'fm-login-id'))
        )
        username_input.send_keys(username)
        
        # 输入密码
        password_input = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.ID, 'fm-login-password'))
        )
        password_input.send_keys(password)
        
        # 点击登录
        login_btn = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.XPATH, '//*[@id="login-form"]/div[4]/button'))
        )
        login_btn.click()
        
        # 等待登录成功
        WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.url_contains('taobao.com')
        )
        
        self.logged_in = True
        print("登录成功")
        return True
        
    except Exception as e:
        print(f"登录失败: {str(e)}")
        return False
        
def load_message_templates(self, file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            self.message_templates = json.load(f)
        print(f"成功加载 {len(self.message_templates)} 条消息模板")
        return True
    except Exception as e:
        print(f"加载消息模板失败: {str(e)}")
        return False
        
def load_contact_list(self, file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            self.contact_list = json.load(f)
        print(f"成功加载 {len(self.contact_list)} 个联系人")
        return True
    except Exception as e:
        print(f"加载联系人列表失败: {str(e)}")
        return False
        
def random_delay(self):
    delay = random.uniform(*self.config['delay_range'])
    time.sleep(delay)
    
def send_single_message(self, contact, message):
    if not self.logged_in:
        print("请先登录")
        return False
        
    try:
        # 打开消息页面
        self.driver.get(f"{self.config['message_url']}sendmsg.htm?toUserId={contact['user_id']}")
        
        # 等待消息框加载
        message_box = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.ID, 'J_Message'))
        )
        
        # 输入消息
        message_box.send_keys(message)
        self.random_delay()
        
        # 点击发送
        send_btn = WebDriverWait(self.driver, self.config['wait_time']).until(
            EC.presence_of_element_located((By.ID, 'J_Send'))
        )
        send_btn.click()
        self.random_delay()
        
        print(f"已发送消息给 {contact['name']}")
        return True
        
    except Exception as e:
        print(f"发送消息给 {contact['name']} 失败: {str(e)}")
        return False
        
def send_mass_messages(self):
    if not self.message_templates or not self.contact_list:
        print("请先加载消息模板和联系人列表")
        return False
        
    success_count = 0
    failure_count = 0
    
    for contact in self.contact_list:
        # 随机选择一条消息模板
        template = random.choice(self.message_templates)
        message = template['content'].format(name=contact['name'])
        
        if self.send_single_message(contact, message):
            success_count += 1
        else:
            failure_count += 1
            
        # 随机延迟,避免触发反爬机制
        self.random_delay()
        
    print(f"发送完成: 成功 {success_count} 条, 失败 {failure_count} 条")
    return True
    
def close(self):
    if self.driver:
        self.driver.quit()
        print("浏览器已关闭")
        

if name == "main": bot = TaobaoMassMessage()

# 登录
if not bot.login("your_username", "your_password"):
    exit()
    
# 加载消息模板
if not bot.load_message_templates("message_templates.json"):
    exit()
    
# 加载联系人列表
if not bot.load_contact_list("contact_list.json"):
    exit()
    
# 开始批量发送
bot.send_mass_messages()

# 关闭浏览器
bot.close()