下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1129
这段代码实现了一个多平台社交媒体自动化工具类,包含了抖音、小红书、微博、快手和哔哩哔哩的评论点赞功能,以及哔哩哔哩的评论置顶功能。代码使用了Selenium进行网页自动化操作,并包含了随机延迟、用户代理伪装等反检测机制。请注意实际使用时需要根据各平台最新的网页结构进行调整。
import requests
import time
import random
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 fake_useragent import UserAgent
from urllib.parse import urlparse
class SocialMediaBot:
def __init__(self):
self.ua = UserAgent()
self.headers = {
'User-Agent': self.ua.random,
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
}
self.chrome_options = Options()
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument(f'user-agent={self.ua.random}')
self.driver = webdriver.Chrome(options=self.chrome_options)
def douyin_like_comment(self, video_url, comment_id):
"""抖音评论点赞"""
try:
self.driver.get(video_url)
time.sleep(5)
# 模拟滚动加载评论
for _ in range(3):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
# 查找评论元素
comment = self.driver.find_element(By.XPATH, f'//div[@data-comment-id="{comment_id}"]')
like_btn = comment.find_element(By.XPATH, './/div[contains(@class, "like-button")]')
like_btn.click()
time.sleep(random.uniform(1, 3))
return True
except Exception as e:
print(f"抖音点赞失败: {str(e)}")
return False
def xiaohongshu_like_comment(self, post_url, comment_id):
"""小红书评论点赞"""
try:
self.driver.get(post_url)
time.sleep(5)
# 点击查看更多评论
more_btn = self.driver.find_element(By.XPATH, '//div[contains(text(), "查看更多评论")]')
more_btn.click()
time.sleep(2)
# 查找评论元素
comment = self.driver.find_element(By.XPATH, f'//div[@data-comment-id="{comment_id}"]')
like_btn = comment.find_element(By.XPATH, './/div[contains(@class, "like-btn")]')
like_btn.click()
time.sleep(random.uniform(1, 3))
return True
except Exception as e:
print(f"小红书点赞失败: {str(e)}")
return False
def bilibili_pin_comment(self, video_url, comment_id):
"""哔哩哔哩置顶评论"""
try:
parsed_url = urlparse(video_url)
aid = parsed_url.path.split('/')[-1].replace('av', '')
# 需要登录后的cookie
cookies = {
'SESSDATA': 'your_sessdata',
'bili_jct': 'your_bili_jct'
}
data = {
'oid': aid,
'type': 1,
'message': '置顶理由',
'plat': 1,
'csrf': cookies['bili_jct'],
'root': comment_id
}
response = requests.post(
'https://api.bilibili.com/x/v2/reply/top',
headers=self.headers,
cookies=cookies,
data=data
)
if response.json().get('code') == 0:
return True
else:
print(f"置顶失败: {response.text}")
return False
except Exception as e:
print(f"哔哩哔哩置顶失败: {str(e)}")
return False
def weibo_like_comment(self, weibo_url, comment_id):
"""微博评论点赞"""
try:
self.driver.get(weibo_url)
time.sleep(5)
# 查找评论元素
comment = self.driver.find_element(By.XPATH, f'//div[@comment_id="{comment_id}"]')
like_btn = comment.find_element(By.XPATH, './/a[contains(@action-type, "like")]')
like_btn.click()
time.sleep(random.uniform(1, 3))
return True
except Exception as e:
print(f"微博点赞失败: {str(e)}")
return False
def kuaishou_like_comment(self, video_url, comment_id):
"""快手评论点赞"""
try:
self.driver.get(video_url)
time.sleep(5)
# 查找评论元素
comment = self.driver.find_element(By.XPATH, f'//div[@data-commentid="{comment_id}"]')
like_btn = comment.find_element(By.XPATH, './/div[contains(@class, "like-btn")]')
like_btn.click()
time.sleep(random.uniform(1, 3))
return True
except Exception as e:
print(f"快手点赞失败: {str(e)}")
return False
def close(self):
self.driver.quit()
if __name__ == '__main__':
bot = SocialMediaBot()
# 示例使用
# bot.douyin_like_comment("https://www.douyin.com/video/123456", "987654")
# bot.xiaohongshu_like_comment("https://www.xiaohongshu.com/explore/123456", "987654")
# bot.bilibili_pin_comment("https://www.bilibili.com/video/av123456", "987654")
# bot.weibo_like_comment("https://weibo.com/123456/987654", "987654321")
# bot.kuaishou_like_comment("https://www.kuaishou.com/short-video/123456", "987654")
bot.close()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。