いるもん
インスト
pip install whoosh
前提条件
Gmailでやりたい時、管理者アカウント作らんと有効にできない管理者アカウント設定方法
このリンクにアクセス↓
https://admin.google.com/
メニュー → [アプリ] > [Google Workspace] > [Gmail] > [エンドユーザーのアクセス]→[POP と IMAP アクセス]
→[どのユーザーも IMAP アクセスを使用できるようにする]
→保存
やる手続き
IMAPをGmailで有効にする
Gmailにログイン → 設定 → すべての設定を表示 →
「転送とPOP/IMAP」→ 「IMAPアクセス」 →
「IMAPを有効にする」 → 「変更を保存」をクリック
code
✅
import imaplib
import email
import requests
from bs4 import BeautifulSoup
# IMAPの設定
IMAP_SERVER = "outlook.office365.com"
EMAIL_ACCOUNT = "your-email@outlook.com"
APP_PASSWORD = "your-app-password" # もし2段階認証を使っている場合のアプリパスワード
# IMAPで接続
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(EMAIL_ACCOUNT, APP_PASSWORD)
mail.select("Drafts") # 下書きフォルダを選択
# "メモ"というタイトルの下書きを検索
status, messages = mail.search(None, 'SUBJECT "メモ"')
# メールIDを取得
email_ids = messages[0].split()
for e_id in email_ids:
status, msg_data = mail.fetch(e_id, "(RFC822)")
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
subject = msg["Subject"]
body = msg.get_payload(decode=True)
text = body.decode("utf-8", errors="ignore") if body else ""
print(f"📄 メモの内容:\n{text}\n")
# メモの内容を検索する
search_query = text.strip() # 検索ワードを取り出す
if search_query:
print(f"検索中: {search_query}")
# 例:Googleで検索
url = f"https://www.google.com/search?q={search_query}"
response = requests.get(url)
# BeautifulSoupでHTMLをパース
soup = BeautifulSoup(response.text, "html.parser")
results = soup.find_all('h3') # 検索結果の見出しを抽出
for result in results:
print(result.get_text()) # 見出しを表示