{"id":1147864,"date":"2025-01-13T16:30:11","date_gmt":"2025-01-13T08:30:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1147864.html"},"modified":"2025-01-13T16:30:13","modified_gmt":"2025-01-13T08:30:13","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e8%87%aa%e5%8a%a8%e5%8a%9e%e5%85%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1147864.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u81ea\u52a8\u529e\u516c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25170024\/4498eb2e-3663-47a0-85d3-a6ef7435eb6c.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u81ea\u52a8\u529e\u516c\" \/><\/p>\n<p><p> \u8981\u5229\u7528Python\u8fdb\u884c\u81ea\u52a8\u529e\u516c\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u7ba1\u7406\u3001\u7f51\u7edc\u722c\u866b\u3001\u90ae\u4ef6\u53d1\u9001<\/strong>\u7b49\u65b9\u5f0f\u3002<strong>\u81ea\u52a8\u5316\u4efb\u52a1<\/strong>\u662f\u5176\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u901a\u8fc7Python\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u80fd\u591f\u5b9e\u73b0\u4efb\u52a1\u7684\u81ea\u52a8\u5316\u5904\u7406\uff0c\u4f8b\u5982\u5b9a\u65f6\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3001\u6279\u91cf\u5904\u7406\u6587\u4ef6\u3001\u81ea\u52a8\u5316\u62a5\u8868\u751f\u6210\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u81ea\u52a8\u5316\u4efb\u52a1<\/strong>\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>schedule<\/code>\u5e93\u6765\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install schedule<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u7f16\u5199\u81ea\u52a8\u5316\u4efb\u52a1\u811a\u672c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def job():<\/p>\n<p>    print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<h2><strong>\u6bcf\u969410\u79d2\u6267\u884c\u4e00\u6b21\u4efb\u52a1<\/strong><\/h2>\n<p>schedule.every(10).seconds.do(job)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9e\u73b0\u4e86\u6bcf\u969410\u79d2\u6267\u884c\u4e00\u6b21\u4efb\u52a1\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u66f4\u6539\u65f6\u95f4\u95f4\u9694\u548c\u4efb\u52a1\u5185\u5bb9\u3002\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u5c06\u9700\u8981\u81ea\u52a8\u5316\u7684\u529e\u516c\u4efb\u52a1\u66ff\u6362\u5230<code>job<\/code>\u51fd\u6570\u4e2d\uff0c\u4f8b\u5982\u81ea\u52a8\u751f\u6210\u62a5\u8868\u3001\u53d1\u9001\u63d0\u9192\u90ae\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Python\u5728\u5176\u4ed6\u65b9\u9762\u8fdb\u884c\u529e\u516c\u81ea\u52a8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u52a8\u5316\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u81ea\u52a8\u5316\u4efb\u52a1\u662f\u5b9e\u73b0\u529e\u516c\u81ea\u52a8\u5316\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u4eba\u5de5\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u901a\u8fc7Python\u4e2d\u7684\u8c03\u5ea6\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4efb\u52a1\u7684\u5b9a\u65f6\u6267\u884c\u548c\u81ea\u52a8\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528schedule\u5e93\u5b9a\u65f6\u6267\u884c\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u5982\u524d\u6587\u6240\u8ff0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>schedule<\/code>\u5e93\u6765\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u3002\u8be5\u5e93\u652f\u6301\u591a\u79cd\u65f6\u95f4\u95f4\u9694\u8bbe\u7f6e\uff0c\u4f8b\u5982\u6bcf\u79d2\u3001\u6bcf\u5206\u949f\u3001\u6bcf\u5929\u7b49\u3002\u901a\u8fc7\u7075\u6d3b\u914d\u7f6e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u529e\u516c\u4efb\u52a1\u7684\u81ea\u52a8\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528APScheduler\u5e93\u8fdb\u884c\u9ad8\u7ea7\u8c03\u5ea6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u8c03\u5ea6\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528APScheduler\u5e93\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install apscheduler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u7f16\u5199\u8c03\u5ea6\u4efb\u52a1\u811a\u672c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>def job():<\/p>\n<p>    print(&quot;\u6267\u884c\u9ad8\u7ea7\u8c03\u5ea6\u4efb\u52a1&quot;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<h2><strong>\u6bcf\u592910\u70b9\u6267\u884c\u4e00\u6b21\u4efb\u52a1<\/strong><\/h2>\n<p>scheduler.add_job(job, &#39;cron&#39;, hour=10)<\/p>\n<p>scheduler.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>APScheduler\u5e93\u652f\u6301\u591a\u79cd\u8c03\u5ea6\u65b9\u5f0f\uff0c\u5305\u62ec\u65e5\u671f\u8c03\u5ea6\u3001\u95f4\u9694\u8c03\u5ea6\u548ccron\u8c03\u5ea6\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5904\u7406\u662f\u529e\u516c\u81ea\u52a8\u5316\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\uff0cPython\u62e5\u6709\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5408\u5e76\u3001\u7edf\u8ba1\u7b49\u64cd\u4f5c\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/h2>\n<p>df.dropna(inplace=True)<\/p>\n<h2><strong>\u6570\u636e\u7edf\u8ba1<\/strong><\/h2>\n<p>summary = df.describe()<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>df.to_csv(&#39;cleaned_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7Pandas\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5404\u79cd\u5904\u7406\uff0c\u6781\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528Numpy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Numpy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528Numpy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u6570\u7ec4\u8fd0\u7b97<\/strong><\/h2>\n<p>result = arr * 2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7Numpy\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u503c\u8ba1\u7b97\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6570\u503c\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6587\u4ef6\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u6587\u4ef6\u7ba1\u7406\u662f\u529e\u516c\u81ea\u52a8\u5316\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u901a\u8fc7Python\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u7684\u521b\u5efa\u3001\u4fee\u6539\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528os\u5e93\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>os\u5e93\u662fPython\u4e2d\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528os\u5e93\u8fdb\u884c\u6587\u4ef6\u7684\u521b\u5efa\u3001\u5220\u9664\u7b49\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u521b\u5efa\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;test.txt&#39;, &#39;w&#39;) as f:<\/p>\n<p>    f.write(&#39;Hello, world!&#39;)<\/p>\n<h2><strong>\u5220\u9664\u6587\u4ef6<\/strong><\/h2>\n<p>os.remove(&#39;test.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7os\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6587\u4ef6\u7ba1\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528shutil\u5e93\u8fdb\u884c\u6587\u4ef6\u590d\u5236\u548c\u79fb\u52a8<\/h4>\n<\/p>\n<p><p>shutil\u5e93\u662fPython\u4e2d\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u6587\u4ef6\u590d\u5236\u3001\u79fb\u52a8\u7b49\u64cd\u4f5c\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528shutil\u5e93\u8fdb\u884c\u6587\u4ef6\u7684\u590d\u5236\u3001\u79fb\u52a8\u7b49\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<h2><strong>\u590d\u5236\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.copy(&#39;source.txt&#39;, &#39;destination.txt&#39;)<\/p>\n<h2><strong>\u79fb\u52a8\u6587\u4ef6<\/strong><\/h2>\n<p>shutil.move(&#39;source.txt&#39;, &#39;destination_folder\/&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7shutil\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u4ef6\u7684\u590d\u5236\u548c\u79fb\u52a8\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6587\u4ef6\u7ba1\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f51\u7edc\u722c\u866b<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u722c\u866b\u662f\u83b7\u53d6\u7f51\u7edc\u6570\u636e\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u901a\u8fc7Python\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u7684\u6293\u53d6\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528requests\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>requests\u5e93\u662fPython\u4e2d\u7528\u4e8e\u8fdb\u884cHTTP\u8bf7\u6c42\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u7684\u6293\u53d6\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5requests\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/www.example.com&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<p>content = response.text<\/p>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7requests\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u7684\u6293\u53d6\uff0c\u83b7\u53d6\u9700\u8981\u7684\u7f51\u7edc\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528BeautifulSoup\u8fdb\u884cHTML\u89e3\u6790<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u662fPython\u4e2d\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6587\u6863\u89e3\u6790\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5BeautifulSoup\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u8fdb\u884cHTML\u89e3\u6790\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u89e3\u6790HTML\u6587\u6863<\/strong><\/h2>\n<p>soup = BeautifulSoup(&#39;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello, world!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#39;, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6807\u7b7e\u5185\u5bb9<\/strong><\/h2>\n<p>text = soup.find(&#39;h1&#39;).text<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7BeautifulSoup\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cHTML\u6587\u6863\u7684\u89e3\u6790\uff0c\u63d0\u53d6\u9700\u8981\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u90ae\u4ef6\u53d1\u9001<\/h3>\n<\/p>\n<p><p>\u90ae\u4ef6\u53d1\u9001\u662f\u529e\u516c\u81ea\u52a8\u5316\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u901a\u8fc7Python\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u90ae\u4ef6\u7684\u81ea\u52a8\u53d1\u9001\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528smtplib\u5e93\u53d1\u9001\u90ae\u4ef6<\/h4>\n<\/p>\n<p><p>smtplib\u662fPython\u4e2d\u7684\u6807\u51c6\u5e93\uff0c\u7528\u4e8e\u53d1\u9001\u90ae\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528smtplib\u5e93\u8fdb\u884c\u90ae\u4ef6\u7684\u53d1\u9001\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smtplib<\/p>\n<p>from em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l.mime.text import MIMEText<\/p>\n<p>from email.mime.multipart import MIMEMultipart<\/p>\n<h2><strong>\u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>msg = MIMEMultipart()<\/p>\n<p>msg[&#39;From&#39;] = &#39;sender@example.com&#39;<\/p>\n<p>msg[&#39;To&#39;] = &#39;recipient@example.com&#39;<\/p>\n<p>msg[&#39;Subject&#39;] = &#39;\u81ea\u52a8\u5316\u90ae\u4ef6&#39;<\/p>\n<p>msg.attach(MIMEText(&#39;\u8fd9\u662f\u901a\u8fc7Python\u53d1\u9001\u7684\u81ea\u52a8\u5316\u90ae\u4ef6\u3002&#39;, &#39;plain&#39;))<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>with smtplib.SMTP(&#39;smtp.example.com&#39;, 587) as server:<\/p>\n<p>    server.starttls()<\/p>\n<p>    server.login(&#39;sender@example.com&#39;, &#39;password&#39;)<\/p>\n<p>    server.send_message(msg)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7smtplib\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u90ae\u4ef6\u7684\u53d1\u9001\uff0c\u5b9e\u73b0\u90ae\u4ef6\u7684\u81ea\u52a8\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528yagmail\u5e93\u7b80\u5316\u90ae\u4ef6\u53d1\u9001<\/h4>\n<\/p>\n<p><p>yagmail\u662f\u4e00\u4e2a\u7b80\u5316\u90ae\u4ef6\u53d1\u9001\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5yagmail\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install yagmail<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528yagmail\u5e93\u8fdb\u884c\u90ae\u4ef6\u7684\u53d1\u9001\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yagmail<\/p>\n<h2><strong>\u53d1\u9001\u90ae\u4ef6<\/strong><\/h2>\n<p>yag = yagmail.SMTP(&#39;sender@example.com&#39;, &#39;password&#39;)<\/p>\n<p>yag.send(&#39;recipient@example.com&#39;, &#39;\u81ea\u52a8\u5316\u90ae\u4ef6&#39;, &#39;\u8fd9\u662f\u901a\u8fc7Python\u53d1\u9001\u7684\u81ea\u52a8\u5316\u90ae\u4ef6\u3002&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7yagmail\u5e93\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u90ae\u4ef6\u7684\u53d1\u9001\uff0c\u63d0\u9ad8\u90ae\u4ef6\u53d1\u9001\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5229\u7528Python\u8fdb\u884c\u529e\u516c\u81ea\u52a8\u5316\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u65b9\u9762\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u5982\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u7ba1\u7406\u3001\u7f51\u7edc\u722c\u866b\u548c\u90ae\u4ef6\u53d1\u9001\u7b49\u3002\u901a\u8fc7\u4f7f\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u548c\u4e30\u5bcc\u7684\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u529e\u516c\u81ea\u52a8\u5316\u9700\u6c42\uff0c\u4ece\u800c\u51cf\u5c11\u4eba\u5de5\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u7075\u6d3b\u8fd0\u7528Python\u8fdb\u884c\u529e\u516c\u81ea\u52a8\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5229\u7528Python\u63d0\u9ad8\u529e\u516c\u6548\u7387\uff1f<\/strong><br \/>Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u901a\u8fc7\u5404\u79cd\u5e93\u548c\u5de5\u5177\u6765\u7b80\u5316\u548c\u81ea\u52a8\u5316\u529e\u516c\u4efb\u52a1\u3002\u6bd4\u5982\uff0c\u4f7f\u7528Pandas\u5904\u7406\u6570\u636e\u8868\u683c\u3001\u4f7f\u7528Selenium\u8fdb\u884c\u7f51\u9875\u81ea\u52a8\u5316\u3001\u4f7f\u7528OpenPyXL\u64cd\u4f5cExcel\u6587\u4ef6\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u51cf\u5c11\u624b\u52a8\u64cd\u4f5c\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>Python\u53ef\u4ee5\u81ea\u52a8\u5316\u54ea\u4e9b\u7c7b\u578b\u7684\u529e\u516c\u4efb\u52a1\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u81ea\u52a8\u5316\u8bb8\u591a\u7c7b\u578b\u7684\u529e\u516c\u4efb\u52a1\uff0c\u5305\u62ec\u6570\u636e\u5206\u6790\u3001\u6587\u4ef6\u7ba1\u7406\u3001\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u3001\u7f51\u9875\u6293\u53d6\u548c\u62a5\u544a\u751f\u6210\u3002\u901a\u8fc7\u7f16\u5199\u811a\u672c\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u91cd\u590d\u6027\u5de5\u4f5c\uff0c\u4ece\u800c\u5c06\u66f4\u591a\u65f6\u95f4\u7528\u4e8e\u9ad8\u4ef7\u503c\u7684\u4efb\u52a1\u3002<\/p>\n<p><strong>\u521d\u5b66\u8005\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u529e\u516c\u81ea\u52a8\u5316\uff1f<\/strong><br \/>\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u53ef\u4ee5\u4ece\u5b66\u4e60\u57fa\u672c\u7684Python\u8bed\u6cd5\u5f00\u59cb\uff0c\u63a5\u7740\u4e86\u89e3\u4e00\u4e9b\u5e38\u7528\u7684\u5e93\uff0c\u5982Pandas\u3001NumPy\u3001Selenium\u548cOpenPyXL\u3002\u8bb8\u591a\u5728\u7ebf\u8bfe\u7a0b\u548c\u6559\u7a0b\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u9010\u6b65\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\uff0c\u7ed3\u5408\u5b9e\u9645\u5de5\u4f5c\u573a\u666f\u8fdb\u884c\u7ec3\u4e60\uff0c\u5c06\u4f1a\u66f4\u5feb\u63d0\u5347\u529e\u516c\u81ea\u52a8\u5316\u7684\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5229\u7528Python\u8fdb\u884c\u81ea\u52a8\u529e\u516c\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u52a8\u5316\u4efb\u52a1\u3001\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u7ba1\u7406\u3001\u7f51\u7edc\u722c\u866b\u3001\u90ae\u4ef6\u53d1\u9001\u7b49\u65b9\u5f0f\u3002\u81ea\u52a8\u5316\u4efb\u52a1\u662f [&hellip;]","protected":false},"author":3,"featured_media":1147868,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147864"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1147864"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147864\/revisions"}],"predecessor-version":[{"id":1147869,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147864\/revisions\/1147869"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1147868"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1147864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1147864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1147864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}