下载地址:https://www.pan38.com/share.php?code=JCnzE 提取密码:7789
这个示例使用了Selenium进行网页自动化操作,包含了登录QQ空间、搜索用户和发送好友请求的基本流程。代码中加入了随机延迟和异常处理来模拟人类操作。请注意实际使用时需要安装Selenium和对应浏览器驱动。
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.driver = webdriver.Chrome(options=self.chrome_options)
self.wait = WebDriverWait(self.driver, 15)
def login(self, qq_number, qq_password):
print("正在打开QQ登录页面...")
self.driver.get("https://qzone.qq.com/")
# 切换到iframe
self.wait.until(EC.frame_to_be_available_and_switch_to_it(
(By.ID, "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(qq_password)
# 点击登录
self.wait.until(EC.element_to_be_clickable(
(By.ID, "login_button"))).click()
print("登录成功,等待页面加载...")
time.sleep(5)
def search_and_add(self, keyword, max_add=10):
print(f"开始搜索关键词: {keyword}")
self.driver.get("https://find.qq.com/")
time.sleep(3)
# 输入搜索关键词
search_box = self.wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, "input.search-input")))
search_box.clear()
search_box.send_keys(keyword)
search_box.send_keys(Keys.RETURN)
time.sleep(3)
# 获取用户列表
user_list = self.wait.until(EC.presence_of_all_elements_located(
(By.CSS_SELECTOR, "div.user-item")))
added_count = 0
for i, user in enumerate(user_list[:max_add]):
try:
# 点击添加按钮
add_btn = user.find_element(By.CSS_SELECTOR, "button.add-btn")
if "已添加" not in add_btn.text:
add_btn.click()
time.sleep(random.uniform(1, 3))
# 处理验证信息弹窗
confirm_btn = self.wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "div.dialog-footer button.btn-primary")))
confirm_btn.click()
time.sleep(random.uniform(2, 4))
added_count += 1
print(f"已发送好友请求给第 {i+1} 个用户")
else:
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密码") # 请替换为实际账号
bot.search_and_add("Python", 5) # 搜索Python相关用户,最多加5个
except Exception as e:
print(f"程序出错: {str(e)}")
finally:
bot.close()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。