import selenium 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 import json import base64 class xhsPublisher(): def __init__(self, driver, cookies_path): self.driver = driver self.cookies_path = cookies_path self.cookies = json.loads(open(self.cookies_path, "r").read()) self.load_cookies() self.WEB_URL = "https://creator.xiaohongshu.com/publish/publish?from=menu" def load_cookies(self): for cookie in self.cookies: # 确保每个cookie都有'sameSite'属性 if True: # if 'sameSite' not in cookie: cookie['sameSite'] = 'Strict' # 或者根据需要设置为'Strict'或'None' self.driver.add_cookie(cookie) return True def open_web(self): self.driver.get(self.WEB_URL) return True def select_upload_type(self, upload_type): ## 这里没改好 if upload_type == "image": # 图文 self.driver.find_element(By.XPATH, "//div[@data-v-16fdb080 and @data-v-a964f0b4 and @class='creator-tab active']").click() elif upload_type == "video": # 视频 self.driver.find_element(By.XPATH, "//div[@data-v-7cbccdb2 and @data-v-08fc0cfe and @class='drag-over']").click() return True def upload_image(self, image_path): image_base64 = self.transbase64(image_path) self.driver.find_element(By.XPATH, "//input[@data-v-4fabe6c5 and @data-v-7cbccdb2-s and @class='upload-input']").send_keys(image_base64) return True def upload_video(self, video_path): video_base64 = self.transbase64(video_path) self.driver.find_element(By.XPATH, "//input[@data-v-4fabe6c5 and @data-v-7cbccdb2-s and @class='upload-input']").send_keys(video_base64) return True def transbase64(self, file_path): with open(file_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8') def publish(self, title, content): self.driver.find_element(By.XPATH, "//input[@class='d-text']").send_keys(title) self.driver.find_element(By.XPATH, "//div[@class='ql-editor ql-blank']").send_keys(content) return True def click_publish(self): self.driver.find_element(By.XPATH, "//button[@data-v-34b0c0bc and @data-v-9bfbb062 and @data-v-1fed608d-s and @type='button' and @class='d-button d-button-large --size-icon-large --size-text-h6 d-button-with-content --color-static bold --color-bg-fill --color-text-paragraph custom-button red publishBtn' and @data-eaglet-imp='true']").click() return True def publish_article(self, title, content, image_path, video_path): self.select_upload_type("image") self.upload_image(image_path) self.publish(title, content) self.click_publish() return True def publish_video(self, title, content, video_path): self.select_upload_type("video") self.upload_video(video_path) self.publish(title, content) self.click_publish() return True def work(self, type, title, content, image_path, video_path): try: self.open_web() self.driver.maximize_window() self.driver.implicitly_wait(500) if type == "article": self.publish_article(title, content, image_path, video_path) elif type == "video": self.publish_video(title, content, video_path) else: raise ValueError("Invalid type") self.driver.quit() except Exception as e: print(e) finally: self.driver.quit() return True if __name__ == "__main__": driver = webdriver.Safari() # 设置浏览器全屏 driver.maximize_window() # 设置模拟位置(经度和纬度) driver.execute_script("navigator.geolocation.getCurrentPosition = function(success) {" "success({coords: {latitude: 37.7749, longitude: -122.4194}});" "};") cookies_path = "/Users/yarrow/Spider/seleniumXH/cookies.json" cookies = json.loads(open(cookies_path, "r").read()) # print(cookies) for cookie in cookies: # 确保每个cookie都有'sameSite'属性 if True: # if 'sameSite' not in cookie: cookie['sameSite'] = 'Strict' # 或者根据需要设置为'Strict'或'None' driver.add_cookie(cookie) driver.get("https://creator.xiaohongshu.com/publish/publish?from=menu") # input("press any key to quit") driver.implicitly_wait(3000) ## 图文 #

拖拽视频到此或点击上传