打造自动化公众号文章生成工具:从网页抓取到AI翻译

278 阅读3分钟

引言

在职业生涯的转折点,我决定专注发展我的公众号,将其打造成一个稳定的收入来源。尽管目前收益不高,但我看到了巨大的潜力和发展空间。为了提高效率,同时保持高质量的内容输出,我开发了一套自动化工具,用于从外文网站抓取文章并进行翻译。本文将详细介绍这个工具的开发过程,希望能为同样对自媒体感兴趣的朋友提供一些启发。

1. 文章抓取

第一步是从目标网站抓取文章内容。我选择使用Python来实现这个功能,主要是因为Python有丰富的网络爬虫库和数据处理工具。

1.1 基本抓取功能

首先,我们实现了一个基本的抓取函数:

import requests
from bs4 import BeautifulSoup
import html

def get_article(url, title_node, title_class, content_node, content_class):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        "Referer": "https://xx.im/",
        "X-Agent": "xx/Web",
        "Content-Type": "application/json",
    }

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 检查HTTP请求状态
        response.encoding = 'utf-8'
    except requests.RequestException as e:
        return {'error': f"请求失败:{str(e)}"}

    try:
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.find(title_node, class_=title_class).get_text(strip=True)
        content = soup.find(content_node, class_=content_class)
        if content:
            content = content.prettify()
            content = html.unescape(content)  # 去除转义符
        else:
            return {'error': '内容节点未找到或为空'}
    except (AttributeError, TypeError) as e:
        return {'error': f"解析失败:{str(e)}"}

    return {'title': title, 'content': content}

77449683-64e0-4022-9566-af419dd7a085-image.png

这个函数使用requests库发送HTTP请求,然后用BeautifulSoup解析HTML内容。它可以根据指定的HTML节点和类名提取文章标题和正文。

1.2 优化数据提取

为了更精确地提取文章结构,我们使用XPath来定位特定的HTML元素:

from lxml import etree

def extract_data(dom, title_xpath, profile_xpath, step_xpath, step_title_xpath, step_item_xpath, step_item_img_xpath, step_item_content_xpath):
    # 提取主标题
    main_title_elements = dom.xpath(title_xpath)
    main_title = main_title_elements[0].text.strip() if main_title_elements else "未找到标题"
    
    # 提取简介
    profile_elements = dom.xpath(profile_xpath)
    profile = " ".join([etree.tostring(p, encoding='unicode', method='html').strip() for p in profile_elements])
    
    # 提取步骤
    steps = []
    step_elements = dom.xpath(step_xpath)

    for step in step_elements:
        step_title = step.xpath(step_title_xpath)[0].text.strip() if step.xpath(step_title_xpath) else "未找到步骤标题"
        
        step_items = []
        step_item_elements = step.xpath(step_item_xpath)

        for item in step_item_elements:
            img_elements = item.xpath(step_item_img_xpath)
            content_elements = item.xpath(step_item_content_xpath)
            
            img_url = img_elements[0].get('data-src') or img_elements[0].get('src') if img_elements else "未找到图片"
            content = etree.tostring(content_elements[0], encoding='unicode', method='html').strip() if content_elements else "未找到内容"
            
            if img_url and img_url != "未找到图片":
                step_items.append({
                    "img": img_url,
                    "content": content
                })
        
        if step_title and step_title != "未找到步骤标题":
            steps.append({
                "title": step_title,
                "step_items": step_items
            })

    return {
        "title": main_title,
        "profile": profile,
        "steps": steps
    }

b06397e9-3b98-453d-b630-a6d05548afc7-image.png

这个函数可以更精确地提取文章的结构化数据,包括标题、简介和步骤等信息。

2. AI翻译

获取到结构化的文章数据后,下一步是进行翻译。我选择了扣子(Coze)作为AI翻译工具,因为它提供了强大的API和灵活的自定义选项。

2.1 创建翻译Bot

在Coze平台上,我创建了一个专门用于文章翻译的Bot。 41042ff9-1d5d-4e0f-abe5-3242ff91d208-image.png

2.2 配置工作流

我设计了一个工作流,包括以下步骤:

  1. 接收输入的文章内容
  2. 分析文章结构
  3. 逐段翻译
  4. 整合翻译结果
  5. 输出最终翻译 6e030320-4a52-48d6-abe5-2575083f42ae-image.png

6dbfaf0a-004c-40b2-8451-86757fed87b2-image.png

2.3 API集成

使用Coze提供的API,我用: 4a88d7c0-f478-4177-87a4-35dff95d1703-image.png

3. 整合与输出

最后一步是将翻译后的内容整合到预设的模板中,生成最终的公众号文章。 0f87176a-4309-46e4-9e2f-54740afebda2-image.png

总结

通过这个自动化工具,我们实现了从文章抓取到AI翻译再到最终排版的全流程自动化。这不仅大大提高了内容生产效率,也应用了Coze bot AI 和工作流。

未来,我计划进一步优化这个工具:

  1. 改进图片处理流程,实现自动上传到图床
  2. 优化翻译API的调用,实现批量翻译以提高效率
  3. 增加内容审核功能,确保文章质量

我希望这个项目能激发更多人对自媒体自动化的兴趣。如果您有任何问题或建议,欢迎在评论区留言