下载地址:https://www.pan38.com/share.php?code=JCnzE 提取密码:7789
请注意这仅是演示代码片段,实际开发需要考虑反爬机制、验证码识别、行为检测等多重安全防护。建议开发者遵守平台规则,任何自动化工具使用前应获得用户明确授权。
完整实现还需要处理:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class WeChatHelper:
def __init__(self):
self.driver = webdriver.Chrome()
def login(self, username, password):
# 模拟登录流程
self.driver.get("https://wx.qq.com")
time.sleep(3)
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()
def get_friends_circle(self, count=10):
# 获取朋友圈内容
posts = []
elements = self.driver.find_elements(By.CLASS_NAME, "fui-cell")
for el in elements[:count]:
posts.append(el.text)
return posts
def post_content(self, text):
# 发布新内容
self.driver.find_element(By.CLASS_NAME, "editor").send_keys(text)
self.driver.find_element(By.CLASS_NAME, "btn-send").click()
import time
import random
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 TimeoutException, NoSuchElementException
class WeChatAutomator:
def __init__(self, config):
self.config = config
self.driver = None
self.login_status = False
self.post_history = []
def init_driver(self):
options = webdriver.ChromeOptions()
options.add_argument('--disable-notifications')
options.add_argument('--disable-infobars')
options.add_argument('--disable-extensions')
self.driver = webdriver.Chrome(options=options)
self.driver.set_window_size(1200, 800)
def safe_find_element(self, by, value, timeout=10):
try:
return WebDriverWait(self.driver, timeout).until(
EC.presence_of_element_located((by, value))
)
except TimeoutException:
print(f"Element not found: {by}={value}")
return None
def login(self):
self.init_driver()
try:
self.driver.get("https://wx.qq.com")
time.sleep(random.uniform(1, 3))
username = self.safe_find_element(By.NAME, "account")
password = self.safe_find_element(By.NAME, "password")
if username and password:
username.send_keys(self.config['username'])
password.send_keys(self.config['password'])
self.safe_find_element(By.ID, "loginBtn").click()
# 等待登录成功
if self.safe_find_element(By.CLASS_NAME, "avatar", 30):
self.login_status = True
print("Login successful")
return True
except Exception as e:
print(f"Login failed: {str(e)}")
return False
def get_friends_posts(self, count=10):
if not self.login_status:
print("Please login first")
return []
try:
self.driver.get("https://wx.qq.com/friends")
time.sleep(random.uniform(2, 4))
posts = []
scroll_count = 0
while len(posts) < count and scroll_count < 5:
elements = self.driver.find_elements(By.CLASS_NAME, "fui-cell")
for el in elements:
if el not in posts:
posts.append({
'content': el.text,
'time': time.strftime("%Y-%m-%d %H:%M:%S")
})
if len(posts) >= count:
break
self.driver.execute_script("window.scrollBy(0, 500)")
time.sleep(random.uniform(1, 2))
scroll_count += 1
return posts[:count]
except Exception as e:
print(f"Get posts failed: {str(e)}")
return []
def auto_post(self, content, images=None):
if not self.login_status:
print("Please login first")
return False
try:
self.driver.get("https://wx.qq.com")
time.sleep(random.uniform(1, 2))
post_btn = self.safe_find_element(By.CLASS_NAME, "editor-trigger")
if post_btn:
post_btn.click()
time.sleep(0.5)
editor = self.safe_find_element(By.CLASS_NAME, "editor-content")
if editor:
editor.send_keys(content)
time.sleep(random.uniform(0.5, 1.5))
if images:
upload_btn = self.safe_find_element(By.CLASS_NAME, "upload-btn")
if upload_btn:
for img in images:
upload_btn.send_keys(img)
time.sleep(random.uniform(1, 2))
submit_btn = self.safe_find_element(By.CLASS_NAME, "btn-send")
if submit_btn:
submit_btn.click()
time.sleep(1)
self.post_history.append({
'content': content,
'time': time.strftime("%Y-%m-%d %H:%M:%S"),
'images': images or []
})
return True
except Exception as e:
print(f"Post failed: {str(e)}")
return False
def close(self):
if self.driver:
self.driver.quit()
self.login_status = False
from wechat_auto import WeChatAutomator
import json
import schedule
import time
def load_config():
with open('config.json') as f:
return json.load(f)
def daily_task(automator):
print("Running daily task...")
# 获取朋友圈内容
posts = automator.get_friends_posts(20)
print(f"Got {len(posts)} posts")
# 自动发布内容
if automator.auto_post("今日自动发布内容"):
print("Post successfully")
def main():
config = load_config()
automator = WeChatAutomator(config)
if automator.login():
# 设置定时任务
schedule.every().day.at("09:00").do(daily_task, automator)
try:
while True:
schedule.run_pending()
time.sleep(60)
except KeyboardInterrupt:
print("Stopping...")
finally:
automator.close()
if __name__ == "__main__":
main()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。