メインコンテンツへスキップ

意外に簡単にできること

    意外に簡単にできることはいくつかあります。以下の例はその一部です。


    1. **ウェブスクレイピング**: 特定のウェブサイトからデータを自動で収集するツールの作成。

       ```python

       import requests

       from bs4 import BeautifulSoup


       url = 'https://example.com'

       response = requests.get(url)

       soup = BeautifulSoup(response.text, 'html.parser')


       for item in soup.find_all('h2'):

           print(item.text)

       ```


    2. **自動メール送信**: Pythonを使って自動でメールを送信するスクリプト。

       ```python

       import smtplib

       from email.mime.text import MIMEText


       def send_email(subject, body, to):

           from_email = "your_email@example.com"

           from_password = "your_password"


           msg = MIMEText(body)

           msg['Subject'] = subject

           msg['From'] = from_email

           msg['To'] = to


           server = smtplib.SMTP_SSL('smtp.example.com', 465)

           server.login(from_email, from_password)

           server.sendmail(from_email, [to], msg.as_string())

           server.quit()


       send_email("Test Subject", "This is a test email", "recipient@example.com")

       ```


    3. **簡単なWebサーバー**: Flaskを使って簡単なWebサーバーを立ち上げる。

       ```python

       from flask import Flask


       app = Flask(__name__)


       @app.route('/')

       def home():

           return "Hello, Flask!"


       if __name__ == "__main__":

           app.run(debug=True)

       ```


    4. **ファイル操作**: ディレクトリ内のファイルを一括でリネームするスクリプト。

       ```python

       import os


       directory = '/path/to/directory'

       for filename in os.listdir(directory):

           if filename.endswith('.txt'):

               os.rename(os.path.join(directory, filename), os.path.join(directory, 'new_' + filename))

       ```


    これらのスクリプトは、特定のタスクを自動化したり、日常的な作業を効率化するために役立ちます。他にも特定の要件があれば教えてください。

     
     

    TWLV32

     
     
    noteのTWLV32の記事のタイトル集をお確かめください noteのアルゴリズムでは見つからない記事がわんさかあります。 OVER 10000記事

    あなたへのおすすめ