下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1133
这段代码模拟了一个社交媒体自动化点赞工具的实现,包含了登录、评论点赞等基本功能。请注意这仅用于技术研究目的,实际使用可能违反平台规则。代码使用了Selenium进行浏览器自动化,支持小红书、抖音、快手和微博平台。
import time
import random
import requests
from bs4 import BeautifulSoup
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 SocialMediaLiker:
def init(self, platform):
self.platform = platform.lower()
self.driver = None
self.session = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
def init_driver(self):
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
self.driver = webdriver.Chrome(options=chrome_options)
def login(self, username, password):
if self.platform == 'xiaohongshu':
self._xiaohongshu_login(username, password)
elif self.platform == 'douyin':
self._douyin_login(username, password)
elif self.platform == 'kuaishou':
self._kuaishou_login(username, password)
elif self.platform == 'weibo':
self._weibo_login(username, password)
else:
raise ValueError("Unsupported platform")
def _xiaohongshu_login(self, username, password):
self.driver.get("https://www.xiaohongshu.com")
time.sleep(2)
login_btn = self.driver.find_element(By.CSS_SELECTOR, ".login-btn")
login_btn.click()
time.sleep(1)
username_input = self.driver.find_element(By.NAME, "username")
password_input = self.driver.find_element(By.NAME, "password")
username_input.send_keys(username)
password_input.send_keys(password)
password_input.send_keys(Keys.RETURN)
time.sleep(5)
def _douyin_login(self, username, password):
self.driver.get("https://www.douyin.com")
time.sleep(2)
login_btn = self.driver.find_element(By.CSS_SELECTOR, ".login-btn")
login_btn.click()
time.sleep(1)
switch_btn = self.driver.find_element(By.CSS_SELECTOR, ".switch-btn")
switch_btn.click()
time.sleep(1)
username_input = self.driver.find_element(By.NAME, "username")
password_input = self.driver.find_element(By.NAME, "password")
username_input.send_keys(username)
password_input.send_keys(password)
password_input.send_keys(Keys.RETURN)
time.sleep(5)
def like_comments(self, post_url, cid=None, count=10):
if self.platform == 'xiaohongshu':
return self._like_xiaohongshu_comments(post_url, cid, count)
elif self.platform == 'douyin':
return self._like_douyin_comments(post_url, cid, count)
elif self.platform == 'kuaishou':
return self._like_kuaishou_comments(post_url, cid, count)
elif self.platform == 'weibo':
return self._like_weibo_comments(post_url, cid, count)
else:
raise ValueError("Unsupported platform")
def _like_xiaohongshu_comments(self, post_url, cid, count):
self.driver.get(post_url)
time.sleep(3)
# Scroll to load comments
for _ in range(3):
self.driver.execute_script("window.scrollBy(0, 500)")
time.sleep(1)
comments = self.driver.find_elements(By.CSS_SELECTOR, ".comment-item")
liked = 0
for comment in comments:
if liked >= count:
break
comment_id = comment.get_attribute("data-cid")
if cid and comment_id != cid:
continue
try:
like_btn = comment.find_element(By.CSS_SELECTOR, ".like-btn")
if "liked" not in like_btn.get_attribute("class"):
like_btn.click()
liked += 1
time.sleep(random.uniform(0.5, 2))
except:
continue
return liked
def close(self):
if self.driver:
self.driver.quit()
if name == "main":
platforms = ['xiaohongshu', 'douyin', 'kuaishou', 'weibo']
print("Available platforms:", ", ".join(platforms))
platform = input("Select platform: ")
username = input("Username: ")
password = input("Password: ")
post_url = input("Post URL: ")
cid = input("Specific comment ID (leave empty for random): ")
count = int(input("Number of likes: "))
liker = SocialMediaLiker(platform)
try:
liker.init_driver()
liker.login(username, password)
result = liker.like_comments(post_url, cid if cid else None, count)
print(f"Successfully liked {result} comments")
except Exception as e:
print(f"Error: {str(e)}")
finally:
liker.close()