【Python实战】AI自动生成小红书文案:3个步骤让我效率提升10倍,完整代码
·
作为自媒体人,每周要写10+篇文案。以前憋3小时写一篇,现在用Python+AI,10分钟搞定。分享完整代码。
一、核心思路
用Python调用DeepSeek API生成文案大纲和内容,自动保存到文件。
二、完整代码
```python
# auto_copywriting.py
import openai
import json
from datetime import datetime
import os
class AutoCopywriter:
def __init__(self):
self.client = openai.OpenAI(
api_key=os.getenv("DEEPSEEK_KEY"),
base_url="https://api.deepseek.com/v1"
)
def generate_outline(self, topic, platform="小红书"):
"""生成文案大纲"""
prompt = f"""
为{platform}平台生成文案大纲,主题:{topic}
要求:口语化、有emoji、短句子、真实感
结构:痛点、解决方案、效果、互动
"""
response = self.client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "你是资深自媒体文案"},
{"role": "user", "content": prompt}
],
temperature=0.7
)
return response.choices[0].message.content
def generate_content(self, outline, platform="小红书"):
"""生成完整文案"""
prompt = f"""
根据大纲生成完整文案:
{outline}
要求:
1. 标题≤18字,带emoji
2. 正文短段落,每句一行
3. 有具体数字、个人经历
4. 结尾互动
5. 加9个话题标签
"""
response = self.client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "你是小红书爆款文案写手"},
{"role": "user", "content": prompt}
],
temperature=0.8
)
return response.choices[0].message.content
def save_copy(self, title, content, output_dir="copies"):
"""保存文案"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
filename = f"{output_dir}/{datetime.now().strftime('%m%d')}_{title[:10]}.txt"
with open(filename, 'w', encoding='utf-8') as f:
f.write(f"标题:{title}\n\n")
f.write(content)
print(f"文案已保存:{filename}")
return filename
# 使用示例
if __name__ == "__main__":
writer = AutoCopywriter()
topic = "AI做Excel效率提升"
# 生成大纲
outline = writer.generate_outline(topic)
print("大纲:", outline)
# 生成文案
content = writer.generate_content(outline)
print("\n文案:", content[:200], "...")
# 保存
writer.save_copy("AI做Excel", content)
三、运行效果
$ python auto_copywriting.py
大纲: 1. 痛点:以前做Excel2小时
2. 解决方案:AI 3步搞定
3. 效果:现在5分钟
4. 互动:你做Excel多久?
文案: AI做Excel😭以前2小时现在5分钟...
文案已保存:copies/0331_AI做Excel.txt
用时:10分钟(以前3小时)
效率提升:18倍
四、踩坑记录
| 坑 | 解决 |
|---|---|
| AI味太重 | 人工改30%,加个人经历 |
| 标题超标 | 自动截断或提示 |
| 内容雷同 | 换temperature参数 |
五、优化建议
1、加模板库:常用文案结构保存
2、自动发布:对接小红书API
3、A/B测试:生成多版对比效果
标签:Python,AI文案,自媒体自动化,DeepSeek,效率工具,实战教程
更多推荐

所有评论(0)