下载地址:https://www.pan38.com/share.php?code=JCnzE 提取密码:7789
使用Selenium自动化QQ加群流程的基本原理,包含登录、搜索群号和加入群聊等功能模块。实际使用时需要注意:1.QQ网页端可能有反自动化机制 2.频繁操作可能导致账号异常 3.需要处理各种验证情况。建议仅用于学习自动化测试技术。
import time
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class QQAutoJoiner:
def __init__(self):
self.chrome_options = Options()
self.chrome_options.add_argument("--disable-notifications")
self.chrome_options.add_argument("--disable-infobars")
self.driver = webdriver.Chrome(options=self.chrome_options)
self.wait = WebDriverWait(self.driver, 20)
def login(self, qq_number, password):
self.driver.get("https://qun.qq.com/")
login_frame = self.wait.until(
EC.presence_of_element_located((By.ID, "login_frame"))
)
self.driver.switch_to.frame(login_frame)
# 切换到账号密码登录
self.wait.until(
EC.element_to_be_clickable((By.ID, "switcher_plogin"))
).click()
# 输入账号密码
self.wait.until(
EC.presence_of_element_located((By.ID, "u"))
).send_keys(qq_number)
self.wait.until(
EC.presence_of_element_located((By.ID, "p"))
).send_keys(password)
# 点击登录
self.wait.until(
EC.element_to_be_clickable((By.ID, "login_button"))
).click()
# 等待登录完成
time.sleep(5)
def join_group(self, group_number):
try:
# 切换到主框架
self.driver.switch_to.default_content()
# 点击加群按钮
join_btn = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".header-join-group"))
)
join_btn.click()
# 输入群号
group_input = self.wait.until(
EC.presence_of_element_located((By.CSS_SELECTOR, "input.group-number-input"))
)
group_input.clear()
group_input.send_keys(group_number)
time.sleep(1)
# 点击搜索
search_btn = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".search-group-btn"))
)
search_btn.click()
time.sleep(2)
# 点击加入群聊
join_btn = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".join-group-btn"))
)
join_btn.click()
time.sleep(2)
# 处理验证问题(如果有)
try:
verify_input = self.driver.find_element(By.CSS_SELECTOR, ".verify-input")
if verify_input:
answer = input("请输入验证问题的答案: ")
verify_input.send_keys(answer)
confirm_btn = self.driver.find_element(By.CSS_SELECTOR, ".verify-confirm")
confirm_btn.click()
time.sleep(2)
except:
pass
return True
except Exception as e:
print(f"加群失败: {str(e)}")
return False
def batch_join(self, group_list, delay=10):
success_count = 0
for group in group_list:
print(f"正在尝试加入群 {group}...")
if self.join_group(group):
success_count += 1
print(f"成功加入群 {group}")
else:
print(f"加入群 {group} 失败")
# 随机延迟
sleep_time = delay + random.randint(-3, 3)
print(f"等待 {sleep_time} 秒后继续...")
time.sleep(sleep_time)
print(f"操作完成,成功加入 {success_count}/{len(group_list)} 个群")
def close(self):
self.driver.quit()
if __name__ == "__main__":
# 使用示例
bot = QQAutoJoiner()
try:
bot.login("你的QQ号", "你的QQ密码")
# 要加入的群列表
groups_to_join = [
"12345678",
"87654321",
"11223344"
]
bot.batch_join(groups_to_join)
finally:
bot.close()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。