下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1130
这个代码使用了Selenium自动化测试工具模拟浏览器操作,实现了从指定QQ群批量添加好友的功能。代码包含登录QQ、定位群成员、发送好友申请等完整流程。使用时需要安装Selenium和对应浏览器驱动,并修改QQ账号和群号参数。
请注意:实际使用时可能会遇到验证码、操作频率限制等问题,且频繁的自动化操作可能导致账号异常。建议仅用于学习研究目的,控制操作频率,避免对他人造成骚扰。
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 QQAutoAdder:
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_qq(self, qq_number, qq_password):
print("正在登录QQ...")
self.driver.get("https://qzone.qq.com/")
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.driver.find_element(By.ID, "p").send_keys(qq_password)
# 点击登录
self.driver.find_element(By.ID, "login_button").click()
time.sleep(5)
def add_friends_from_group(self, group_number, max_add=10):
print(f"正在从群{group_number}添加好友...")
self.driver.get(f"https://qun.qq.com/member.html#gid={group_number}")
time.sleep(5)
# 获取成员列表
members = self.wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, ".member-list .member-item")))
added_count = 0
for i, member in enumerate(members[:max_add]):
try:
# 点击成员头像
avatar = member.find_element(By.CSS_SELECTOR, ".avatar")
avatar.click()
time.sleep(1)
# 点击加好友按钮
add_btn = self.wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".op-add-friend")))
add_btn.click()
time.sleep(1)
# 填写验证信息
verify_input = self.wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "textarea.verify-msg")))
verify_input.clear()
verify_input.send_keys("你好,交个朋友吧!")
time.sleep(1)
# 提交申请
submit_btn = self.driver.find_element(By.CSS_SELECTOR, ".btn-submit")
submit_btn.click()
time.sleep(random.uniform(2, 5))
added_count += 1
print(f"已发送好友申请给第{i+1}个成员")
except Exception as e:
print(f"添加第{i+1}个成员时出错: {str(e)}")
continue
print(f"已完成,共发送{added_count}个好友申请")
def close(self):
self.driver.quit()
if __name__ == "__main__":
bot = QQAutoAdder()
try:
bot.login_qq("你的QQ号", "你的QQ密码")
bot.add_friends_from_group("群号", max_add=20)
finally:
bot.close()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。