通过AI大模型现实小红书笔记克隆以及自动化发布


前言

对于文案小白来说,通过大模型可以轻松帮我们生成各种风格的文案,比如小红书风格的超萌文案。只需要简单几步操作,就能得到让你惊艳的结果。通过自动化的操作,还可以减少内容维护的重复性劳动,极大提高工作效率。


一、实现思路

通过playwright登录浏览相关笔记,选择需要克隆的笔记,调用扣子的工作流API生成新的文案,选择文案后可以一键自动发布笔记。

二、实现步骤

1.引入库

const fs = require('node:fs');
import { firefox } from 'playwright';
import axios from 'axios';

fs:用于文件系统操作,主要用于读取和写入文件。
playwright:用于自动化浏览器操作,这里使用了其中的firefox浏览器驱动。
axios:用于发送 HTTP 请求。

2.自动登录

  async login() {
    if (!this.currentPage) {
      const storageStatePath = './storageState/xiaohongshu.json';
      let storageState = {};
      if (fs.existsSync(storageStatePath)) {
        storageState = JSON.parse(fs.readFileSync(storageStatePath, 'utf-8'));
      }
      const browser = await firefox.launch({
        headless: false,
      }); // Or 'firefox' or 'webkit'.
      const context = await browser.newContext({ storageState: storageState });
      context.setDefaultTimeout(60 * 1000);
      const page = await context.newPage();
      await page.goto('https://www.xiaohongshu.com/login').catch((error) => {
        console.error(error);
      });
      await page.waitForURL(/https:\/\/www.xiaohongshu.com\/explore.*/);
      await context.storageState({ path: storageStatePath });
      //await browser.close();
      console.log('waitForURL');
      this.currentPage = page;
      page.on('close', (data) => {
        console.log('page close');
        this.currentPage = undefined;
      });
      page.on('response', (response) => {
        if (
          response
            .url()
            .includes('https://edith.xiaohongshu.com/api/sns/web/v1/feed')
        ) {
          response.json().then(async (data: any) => {
            console.log(data.data.items);
            this.currentNote = data.data.items[0];
          });
        }
      });
      return { open: true };
    } else {
      return this.currentNote;
    }
  }

通过storageStatePath存储登录信息便于下次自动登录。page.on(‘response’, (response) => {})监听当前浏览的笔记内容。

3.生成笔记

  async clone() {
    const locatorTitle = await this.currentPage.locator('#detail-title');
    const title = await locatorTitle.innerText();

    const locatorDesc = await this.currentPage.locator('#detail-desc');
    const desc = await locatorDesc.innerText();
    console.log('content', title, desc);
    const response = await axios.post(
      'https://api.coze.cn/v1/workflow/run',
      {
        workflow_id: 'xxx',
        parameters: {
          titile: title,
          desc: desc,
        },
      },
      {
        withCredentials: true, // 这会携带 cookie
        headers: {
          'Content-Type': 'application/json',
          Authorization: 'Bearer xxx',
        },
      },
    );
    //console.log(response);
    const docs = JSON.parse(response.data.data);
    return docs;
  }

使用axios.post向https://api.coze.cn/v1/workflow/run发送 POST 请求,获取大模型生成的内容。携带工作流 ID 和参数(标题和描述),请求中包含的授权信息可以通过coze的后台获取。

4.发布笔记

async upload(note) {
    if (this.currentPage) {
      const page = this.currentPage;
      await page.goto(
        'https://creator.xiaohongshu.com/publish/publish?source=official',
      );
      const uploadImgTab = page.getByText('上传图文');
      await uploadImgTab.waitFor({ state: 'visible', timeout: 20000 });
      await uploadImgTab.click();
      await page.setInputFiles(
        'input.upload-input','',
      );

      const title = page.getByPlaceholder('填写标题会有更多赞哦~');
      await title.waitFor({ state: 'visible', timeout: 20000 });
      await title.fill(note.title);

      const desc = page.getByPlaceholder(
        '在这里输入正文描述,真诚有价值的分享予人温暖',
      );
      await desc.waitFor({ state: 'visible', timeout: 20000 });
      await desc.fill(note.desc);

      const publish = page.locator('button.publishBtn');
      await publish.waitFor({ state: 'visible', timeout: 20000 });
      await publish.click({ delay: 200 });
      return { status: 'ok' };
    }
  }

导航到小红书的发布页面https://creator.xiaohongshu.com/publish/publish?source=official。
等待并点击 “上传图文” 按钮。通过page.setInputFiles方法上传图片文件,这里指定了图片文件的本地路径。获取并填写标题输入框,使用note.title作为标题内容。获取并填写描述输入框,使用note.desc作为描述内容。获取并点击发布按钮,这里有一个 200 毫秒的延迟来确保操作的稳定性。


三、界面演示

在这里插入图片描述

总结

本文展示了如何使用playwright和axios进行自动化操作和网络请求,实现自动化登录、内容获取、大模型内容生成,内容上传等功能。欢迎添加vx:sas-soft沟通交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值