競合他社のWebサイトを毎日チェックするのは大変ですよね。
今回はPythonとOpenAI(ChatGPT)を活用して、競合サイトの更新を自動検知・要約し、Slackに通知する仕組みを構築していきます。
✅ このツールでできること
- 指定したURLのHTMLを定期的に取得
- 前回と異なる差分だけを抽出
- ChatGPTで自然言語による要約
- Slackに自動通知(Webhook経由)
🔧 必要なライブラリ
pip install requests beautifulsoup4 openai slack_sdk
💡 サンプルコード
import requests
from bs4 import BeautifulSoup
import openai
from slack_sdk.webhook import WebhookClient
import difflib
import os
# 環境変数などに保存しておくのが安全です
SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/xxxx"
openai.api_key = "your_openai_key"
# 対象の競合サイトURL
url = "https://example.com/news"
cache_file = "last_content.txt"
# Webページ取得&整形
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
text = soup.get_text().strip()
# 前回との差分確認
if os.path.exists(cache_file):
with open(cache_file, "r", encoding="utf-8") as f:
old_text = f.read()
else:
old_text = ""
diff = "\n".join(difflib.unified_diff(old_text.splitlines(), text.splitlines()))
# 差分があれば要約とSlack通知
if diff.strip():
prompt = f"以下のテキストの変更内容を簡潔に日本語で要約してください:\n\n{diff}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
summary = response['choices'][0]['message']['content']
webhook = WebhookClient(SLACK_WEBHOOK_URL)
webhook.send(text=f"🔔 競合サイトの更新検知!\n\n{summary}")
# 現在の内容を保存
with open(cache_file, "w", encoding="utf-8") as f:
f.write(text)
else:
print("更新はありませんでした。")
📌 注意点
- HTMLの構造が頻繁に変わるサイトには不向きです
- ChatGPTトークン量により長文の要約は制限があります
- Slack Webhookの設定が必要です
📚 ビジネス活用例
- 競合商品ページの更新チェック
- 企業ブログの新着記事モニタリング
- ニュースサイトの業界動向把握
▶️ 次回予告
Day37では、「Pythonで営業リストを自動作成!Googleマップ×OpenAIで地域ターゲティング」をテーマにお送りします。
コメント