自动小红书发布笔记工具,小红书作品视频自动发布软件,易语言免费版下载
下载地址:https://pan38.com/share.php?code=vAWwa 提取码:bilibili
源码部分:
`
import os
import time
import json
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.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from PIL import Image
import cv2
import numpy as np
class XHSAutoPoster:
def init(self):
self.driver = None
self.cookies_file = "xhs_cookies.json"
self.config = {
"headless": False,
"proxy": None,
"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",
"wait_time": 30,
"implicit_wait": 10
}
def init_driver(self):
chrome_options = Options()
if self.config["headless"]:
chrome_options.add_argument("--headless")
if self.config["proxy"]:
chrome_options.add_argument(f"--proxy-server={self.config['proxy']}")
chrome_options.add_argument(f"user-agent={self.config['user_agent']}")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)
self.driver = webdriver.Chrome(options=chrome_options)
self.driver.implicitly_wait(self.config["implicit_wait"])
def login(self, phone, password):
self.driver.get("https://creator.xiaohongshu.com/login")
# 输入手机号
phone_input = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.presence_of_element_located((By.XPATH, "//input[@placeholder='请输入手机号']"))
)
self.slow_type(phone_input, phone)
# 点击获取验证码
get_code_btn = self.driver.find_element(By.XPATH, "//button[contains(text(),'获取验证码')]")
get_code_btn.click()
# 等待用户输入验证码
code = input("请输入收到的验证码: ")
code_input = self.driver.find_element(By.XPATH, "//input[@placeholder='请输入验证码']")
self.slow_type(code_input, code)
# 点击登录
login_btn = self.driver.find_element(By.XPATH, "//button[contains(text(),'登录')]")
login_btn.click()
# 保存cookies
self.save_cookies()
def save_cookies(self):
cookies = self.driver.get_cookies()
with open(self.cookies_file, "w") as f:
json.dump(cookies, f)
def load_cookies(self):
if os.path.exists(self.cookies_file):
with open(self.cookies_file, "r") as f:
cookies = json.load(f)
for cookie in cookies:
self.driver.add_cookie(cookie)
return True
return False
def post_image_note(self, title, content, image_paths):
self.driver.get("https://creator.xiaohongshu.com/publish/publish")
# 输入标题
title_input = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.presence_of_element_located((By.XPATH, "//textarea[@placeholder='填写标题']"))
)
self.slow_type(title_input, title)
# 输入内容
content_div = self.driver.find_element(By.XPATH, "//div[@class='editor']")
content_div.click()
self.slow_type(content_div, content)
# 上传图片
for img_path in image_paths:
file_input = self.driver.find_element(By.XPATH, "//input[@type='file']")
file_input.send_keys(os.path.abspath(img_path))
time.sleep(3) # 等待上传完成
# 点击发布
publish_btn = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'publish-btn')]"))
)
publish_btn.click()
return True
def post_video_note(self, title, content, video_path, cover_path=None):
self.driver.get("https://creator.xiaohongshu.com/publish/publish?type=video")
# 上传视频
file_input = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.presence_of_element_located((By.XPATH, "//input[@type='file']"))
)
file_input.send_keys(os.path.abspath(video_path))
# 监控上传进度
while True:
try:
progress = self.driver.find_element(By.XPATH, "//div[contains(@class,'progress')]")
if "100%" in progress.text:
break
time.sleep(2)
except:
break
# 输入标题
title_input = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.presence_of_element_located((By.XPATH, "//textarea[@placeholder='填写标题']"))
)
self.slow_type(title_input, title)
# 输入内容
content_div = self.driver.find_element(By.XPATH, "//div[@class='editor']")
content_div.click()
self.slow_type(content_div, content)
# 等待转码完成
time.sleep(30)
# 点击发布
publish_btn = WebDriverWait(self.driver, self.config["wait_time"]).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(@class,'publish-btn')]"))
)
publish_btn.click()
return True
def slow_type(self, element, text, delay=0.1):
for character in text:
element.send_keys(character)
time.sleep(random.uniform(delay/2, delay*1.5))
def human_like_scroll(self):
for _ in range(random.randint(2, 5)):
scroll_px = random.randint(200, 800)
self.driver.execute_script(f"window.scrollBy(0, {scroll_px})")
time.sleep(random.uniform(0.5, 2.0))
def close(self):
if self.driver:
self.driver.quit()
def __del__(self):
self.close()
if name == "main":
poster = XHSAutoPoster()
poster.init_driver()
# 尝试加载cookies
if not poster.load_cookies():
# 需要登录
poster.login("your_phone_number", "your_password")
# 发布图文笔记示例
poster.post_image_note(
"测试自动发布图文笔记",
"这是通过Python脚本自动发布的小红书图文笔记内容 #自动化 #小红书运营",
["image1.jpg", "image2.jpg"]
)
# 发布视频笔记示例
poster.post_video_note(
"测试自动发布视频笔记",
"这是通过Python脚本自动发布的小红书视频笔记内容 #视频 #自动化",
"test_video.mp4"
)
poster.close()
`
浙公网安备 33010602011771号