下载地址:https://www.pan38.com/yun/share.php?code=JCnzE 提取密码:9995
这个淘宝私信群发工具主要包含以下功能:
使用Selenium自动化登录淘宝账号
通过阿里旺旺网页版发送私信
支持从CSV文件批量读取客户信息
支持消息模板,可自定义变量
添加随机延迟防止被封号
完整的错误处理和日志记录
使用前需要安装依赖:pip install selenium pandas
源码部分:【仅供学习参考】
import time
import random
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 TaobaoMassMessenger:
def init(self, username, password):
self.username = username
self.password = password
self.driver = None
self.wait_time = 10
self.message_delay = (3, 7) # 随机延迟范围(秒)
def init_driver(self):
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument("--disable-infobars")
options.add_argument("--start-maximized")
self.driver = webdriver.Chrome(options=options)
self.driver.implicitly_wait(self.wait_time)
def login(self):
self.driver.get("https://login.taobao.com/")
# 切换到密码登录
try:
password_login = WebDriverWait(self.driver, self.wait_time).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(text(),'密码登录')]"))
)
password_login.click()
except Exception as e:
print("无法找到密码登录选项:", e)
return False
# 输入用户名和密码
try:
username_input = WebDriverWait(self.driver, self.wait_time).until(
EC.presence_of_element_located((By.ID, "fm-login-id"))
)
password_input = self.driver.find_element(By.ID, "fm-login-password")
username_input.clear()
username_input.send_keys(self.username)
time.sleep(1)
password_input.clear()
password_input.send_keys(self.password)
time.sleep(1)
# 点击登录按钮
login_button = self.driver.find_element(By.XPATH, "//button[contains(text(),'登 录')]")
login_button.click()
# 等待登录成功
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.ID, "q"))
)
return True
except Exception as e:
print("登录失败:", e)
return False
def send_message_to_user(self, username, message):
try:
# 打开旺旺聊天窗口
self.driver.get(f"https://amos.alicdn.com/msg.aw?v=2&uid={username}&site=cntaobao&s=1&charset=utf-8")
# 等待聊天窗口加载完成
WebDriverWait(self.driver, self.wait_time).until(
EC.presence_of_element_located((By.ID, "J_Textarea"))
)
# 输入消息
textarea = self.driver.find_element(By.ID, "J_Textarea")
textarea.clear()
for line in message.split('\n'):
textarea.send_keys(line)
textarea.send_keys(Keys.SHIFT, Keys.ENTER)
time.sleep(1)
# 发送消息
send_button = self.driver.find_element(By.ID, "J_SendBtn")
send_button.click()
# 随机延迟
time.sleep(random.uniform(*self.message_delay))
return True
except Exception as e:
print(f"发送消息给 {username} 失败:", e)
return False
def send_messages_from_csv(self, csv_file, message_template):
try:
df = pd.read_csv(csv_file)
success_count = 0
fail_count = 0
for index, row in df.iterrows():
username = row['username']
custom_message = message_template.format(**row.to_dict())
print(f"正在发送消息给 {username}...")
if self.send_message_to_user(username, custom_message):
success_count += 1
print(f"成功发送消息给 {username}")
else:
fail_count += 1
print(f"发送消息给 {username} 失败")
# 随机延迟防止被封
time.sleep(random.uniform(5, 15))
print(f"\n发送完成: 成功 {success_count} 条, 失败 {fail_count} 条")
return True
except Exception as e:
print("发送消息过程中出错:", e)
return False
def close(self):
if self.driver:
self.driver.quit()
if name == "main":
# 配置信息
USERNAME = "your_taobao_username"
PASSWORD = "your_taobao_password"
CSV_FILE = "customers.csv" # 包含username列和其他自定义列的CSV文件
MESSAGE_TEMPLATE = """亲爱的{username}:
感谢您对我们店铺的支持!我们最近推出了新品{product_name},原价{original_price}元,现在特价仅需{discount_price}元!
点击链接查看详情:{product_link}
有任何问题欢迎随时咨询哦~"""
# 创建并运行发送器
messenger = TaobaoMassMessenger(USERNAME, PASSWORD)
try:
messenger.init_driver()
if messenger.login():
messenger.send_messages_from_csv(CSV_FILE, MESSAGE_TEMPLATE)
except Exception as e:
print("程序运行出错:", e)
finally:
messenger.close()
username,product_name,original_price,discount_price,product_link
customer1,新款T恤,199,159,https://item.taobao.com/item1
customer2,休闲裤,299,239,https://item.taobao.com/item2
customer3,运动鞋,599,499,https://item.taobao.com/item3