0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

outlook メール下書き使って、ネット検索

Last updated at Posted at 2025-04-01

いるもん

インスト
 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())  # 見出しを表示

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?