下载地址:https://www.pan38.com/share.php?code=pvvmX 提取码:8888
必看声明:仅用于学习用途,下载后24小时请删除!
import time
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
class WechatAutoHelper:
def __init__(self):
self.driver = webdriver.Chrome()
self.wait_time = random.uniform(2.0, 5.0)
def login(self, username, password):
# 模拟登录流程
self.driver.get("https://web.wechat.com")
time.sleep(self.wait_time)
self.driver.find_element(By.NAME, "account").send_keys(username)
self.driver.find_element(By.NAME, "password").send_keys(password)
self.driver.find_element(By.ID, "loginBtn").click()
time.sleep(10) # 等待扫码登录
def add_contact(self, wxid, greeting):
# 模拟添加联系人
self.driver.get(f"https://web.wechat.com/{wxid}")
time.sleep(self.wait_time)
self.driver.find_element(By.CLASS_NAME, "add-btn").click()
time.sleep(1)
self.driver.find_element(By.TAG_NAME, "textarea").send_keys(greeting)
self.driver.find_element(By.CLASS_NAME, "send-btn").click()
def batch_add(self, contacts):
# 批量添加逻辑
for name, wxid in contacts.items():
try:
self.add_contact(wxid, f"您好{name},希望能认识您")
print(f"已发送好友请求给: {name}")
time.sleep(random.randint(15, 60)) # 随机间隔
except Exception as e:
print(f"添加失败: {name} - {str(e)}")
import time
import random
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import (NoSuchElementException,
TimeoutException,
StaleElementReferenceException)
class WechatAutomation:
def __init__(self, config_path='config.json'):
with open(config_path) as f:
self.config = json.load(f)
# 初始化浏览器驱动
options = webdriver.ChromeOptions()
options.add_argument(f'user-agent={self.config["user_agent"]}')
self.driver = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.driver, 15)
# 行为参数
self.min_delay = self.config.get('min_delay', 3)
self.max_delay = self.config.get('max_delay', 10)
self.max_retries = self.config.get('max_retries', 3)
def random_delay(self):
time.sleep(random.uniform(self.min_delay, self.max_delay))
def safe_click(self, locator):
for _ in range(self.max_retries):
try:
element = self.wait.until(
EC.element_to_be_clickable(locator)
)
element.click()
self.random_delay()
return True
except Exception as e:
print(f"点击失败: {str(e)}")
time.sleep(2)
return False
def login(self):
self.driver.get("https://web.wechat.com")
try:
# 等待二维码出现
qr_code = self.wait.until(
EC.presence_of_element_located(
(By.CLASS_NAME, "qr-code-img")
)
)
print("请扫描二维码登录...")
self.wait.until(
lambda d: "https://web.wechat.com/" in d.current_url
)
print("登录成功")
return True
except TimeoutException:
print("登录超时")
return False
def add_contact(self, wxid, greeting_template):
try:
# 访问用户主页
self.driver.get(f"https://web.wechat.com/{wxid}")
# 点击添加按钮
if not self.safe_click((By.CLASS_NAME, "add-btn")):
raise Exception("找不到添加按钮")
# 填写验证消息
greeting = greeting_template.replace("${wxid}", wxid)
textarea = self.wait.until(
EC.presence_of_element_located(
(By.TAG_NAME, "textarea")
)
)
textarea.clear()
textarea.send_keys(greeting)
# 发送请求
if not self.safe_click((By.CLASS_NAME, "send-btn")):
raise Exception("找不到发送按钮")
print(f"成功发送好友请求给: {wxid}")
return True
except Exception as e:
print(f"添加失败[{wxid}]: {str(e)}")
return False
def batch_add(self, contacts_file):
with open(contacts_file) as f:
contacts = [line.strip() for line in f if line.strip()]
success = 0
for idx, wxid in enumerate(contacts, 1):
print(f"\n处理进度: {idx}/{len(contacts)}")
if self.add_contact(wxid, self.config["greeting"]):
success += 1
time.sleep(random.randint(20, 60)) # 随机间隔
print(f"\n任务完成: 成功{success}个, 失败{len(contacts)-success}个")
def close(self):
self.driver.quit()
if __name__ == "__main__":
tool = WechatAutomation()
try:
if tool.login():
tool.batch_add("contacts.txt")
finally:
tool.close()
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"min_delay": 3,
"max_delay": 8,
"max_retries": 3,
"greeting": "您好${wxid},我是XX公司的商务代表,希望能与您建立联系"
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。