{"id":205177,"date":"2025-05-29T12:37:19","date_gmt":"2025-05-29T04:37:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205177\/"},"modified":"2025-05-29T12:37:19","modified_gmt":"2025-05-29T04:37:19","slug":"%e8%87%aa%e5%8a%a8%e6%89%a7%e8%a1%8c%e6%97%a5%e5%b8%b8%e4%bb%bb%e5%8a%a1%e7%9a%84-python-%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205177\/","title":{"rendered":"\u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\u7684 Python \u811a\u672c"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\u7684 Python \u811a\u672c<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\u7684 Python \u811a\u672c\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/1732520360674429a85c251.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6bcf\u4e2a\u4eba\u90fd\u5fc5\u987b\u62e5\u6709\u7684\u6536\u85cf&#8230;&#8230;<\/p>\n<p>python \u51ed\u501f\u5176\u7b80\u5355\u6027\u548c\u5f3a\u5927\u7684\u5e93\u6539\u53d8\u4e86\u6211\u4eec\u5b9e\u73b0\u81ea\u52a8\u5316\u7684\u65b9\u5f0f\u3002\u65e0\u8bba\u60a8\u662f\u6280\u672f\u7231\u597d\u8005\u3001\u5fd9\u788c\u7684\u4e13\u4e1a\u4eba\u58eb\uff0c\u8fd8\u662f\u53ea\u662f\u60f3\u7b80\u5316\u65e5\u5e38\u5de5\u4f5c\uff0cpython \u90fd\u53ef\u4ee5\u5e2e\u52a9\u60a8\u81ea\u52a8\u6267\u884c\u91cd\u590d\u6027\u4efb\u52a1\uff0c\u8282\u7701\u65f6\u95f4\u5e76\u63d0\u9ad8\u6548\u7387\u3002\u8fd9\u91cc\u6536\u96c6\u4e86 10 \u4e2a\u57fa\u672c\u7684 python \u811a\u672c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u81ea\u52a8\u5316\u65e5\u5e38\u751f\u6d3b\u7684\u5404\u4e2a\u65b9\u9762\u3002 <\/p>\n<p>\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n<hr>\n<p>1.\u81ea\u52a8\u53d1\u9001\u7535\u5b50\u90ae\u4ef6<\/p>\n<p>\u624b\u52a8\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5c24\u5176\u662f\u91cd\u590d\u53d1\u9001\u7684\u7535\u5b50\u90ae\u4ef6\uff0c\u53ef\u80fd\u975e\u5e38\u8017\u65f6\u3002\u4f7f\u7528 python \u7684 smtplib \u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u81ea\u52a8\u5316\u6b64\u8fc7\u7a0b\u3002\u65e0\u8bba\u662f\u53d1\u9001\u63d0\u9192\u3001\u66f4\u65b0\u8fd8\u662f\u4e2a\u6027\u5316\u6d88\u606f\uff0c\u8fd9\u4e2a\u811a\u672c\u90fd\u53ef\u4ee5\u5904\u7406\u3002<\/p>\n<pre>import smtplib\nfrom email.mime.text import mimetext\nfrom email.mime.multipart import mimemultipart\n\ndef send_email(receiver_email, subject, body):\n    sender_email = \"your_email@example.com\"\n    password = \"your_password\"\n\n    msg = mimemultipart()\n    msg['from'] = sender_email\n    msg['to'] = receiver_email\n    msg['subject'] = subject\n    msg.attach(mimetext(body, 'plain'))\n\n    try:\n        with smtplib.smtp('smtp.gmail.com', 587) as server:\n            server.starttls()\n            server.login(sender_email, password)\n            server.sendmail(sender_email, receiver_email, msg.as_string())\n            print(\"email sent successfully!\")\n    except exception as e:\n        print(f\"error: {e}\")\n\n# example usage\nsend_email(\"receiver_email@example.com\", \"subject here\", \"email body goes here.\")\n\n<\/pre>\n<p>\u6b64\u811a\u672c\u53ef\u4ee5\u8f7b\u677e\u96c6\u6210\u5230\u66f4\u5927\u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\uff0c\u4f8b\u5982\u53d1\u9001\u62a5\u544a\u6216\u8b66\u62a5\u3002<\/p>\n<p>2.\u6587\u4ef6\u7ba1\u7406\u5668<\/p>\n<p>\u5982\u679c\u60a8\u7684\u4e0b\u8f7d\u6587\u4ef6\u5939\u4e00\u7247\u6df7\u4e71\uff0c\u90a3\u4e48\u8fd9\u4e2a\u811a\u672c\u9002\u5408\u60a8\u3002\u5b83\u6309\u6269\u5c55\u540d\u7ec4\u7ec7\u6587\u4ef6\uff0c\u5c06\u5b83\u4eec\u6574\u9f50\u5730\u653e\u5165\u5b50\u6587\u4ef6\u5939\u4e2d\u3002\u4e0d\u518d\u9700\u8981\u7b5b\u9009\u6570\u5341\u4e2a\u6587\u4ef6\u6765\u627e\u5230\u60a8\u9700\u8981\u7684\u5185\u5bb9\uff01<\/p>\n<pre>import os\nfrom shutil import move\n\ndef organize_folder(folder_path):\n    for file in os.listdir(folder_path):\n        if os.path.isfile(os.path.join(folder_path, file)):\n            ext = file.split('.')[-1]\n            ext_folder = os.path.join(folder_path, ext)\n            os.makedirs(ext_folder, exist_ok=true)\n            move(os.path.join(folder_path, file), os.path.join(ext_folder, file))\n\n# example usage\norganize_folder(\"c:\/users\/yourname\/downloads\")\n\n<\/pre>\n<p>\u6b64\u811a\u672c\u5bf9\u4e8e\u7ba1\u7406 pdf\u3001\u56fe\u50cf\u6216\u6587\u6863\u7b49\u6587\u4ef6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p>3.\u7f51\u9875\u6293\u53d6\u65b0\u95fb\u5934\u6761<\/p>\n<p>\u901a\u8fc7\u4ece\u60a8\u6700\u559c\u7231\u7684\u7f51\u7ad9\u6293\u53d6\u5934\u6761\u65b0\u95fb\u6765\u4e86\u89e3\u6700\u65b0\u65b0\u95fb\u3002 python \u7684\u201crequests\u201d\u548c\u201cbeautifulsoup\u201d\u5e93\u4f7f\u8fd9\u4e2a\u8fc7\u7a0b\u53d8\u5f97\u65e0\u7f1d\u3002<\/p>\n<pre>import requests\nfrom bs4 import beautifulsoup\n\ndef fetch_headlines(url):\n    response = requests.get(url)\n    soup = beautifulsoup(response.content, \"html.parser\")\n    headlines = [h.text for h in soup.find_all('h2', class_='headline')]\n    return headlines\n\n# example usage\nheadlines = fetch_headlines(\"https:\/\/news.ycombinator.com\/\")\nprint(\"\\n\".join(headlines))\n\n<\/pre>\n<p>\u65e0\u8bba\u60a8\u662f\u65b0\u95fb\u8ff7\u8fd8\u662f\u9700\u8981\u5de5\u4f5c\u66f4\u65b0\uff0c\u6b64\u811a\u672c\u90fd\u53ef\u4ee5\u5b89\u6392\u6bcf\u5929\u8fd0\u884c\u3002<\/p>\n<p>4.\u6bcf\u65e5\u5929\u6c14\u901a\u77e5<\/p>\n<p>\u4ece\u5929\u6c14\u66f4\u65b0\u5f00\u59cb\u65b0\u7684\u4e00\u5929\uff01\u6b64\u811a\u672c\u4f7f\u7528 openweathermap api \u83b7\u53d6\u60a8\u6240\u5728\u57ce\u5e02\u7684\u5929\u6c14\u6570\u636e\u5e76\u663e\u793a\u6e29\u5ea6\u548c\u5929\u6c14\u9884\u62a5\u3002<\/p>\n<pre>import requests\n\ndef get_weather(city):\n    api_key = \"your_api_key\"\n    url = f\"http:\/\/api.openweathermap.org\/data\/2.5\/weather?q={city}&amp;appid={api_key}&amp;units=metric\"\n    response = requests.get(url).json()\n    if response.get(\"main\"):\n        temp = response['main']['temp']\n        weather = response['weather'][0]['description']\n        print(f\"the current weather in {city} is {temp}\u00b0c with {weather}.\")\n    else:\n        print(\"city not found!\")\n\n# example usage\nget_weather(\"new york\")\n\n<\/pre>\n<p>\u53ea\u9700\u7a0d\u52a0\u8c03\u6574\uff0c\u60a8\u5c31\u53ef\u4ee5\u8ba9\u5b83\u76f4\u63a5\u5411\u60a8\u7684\u624b\u673a\u53d1\u9001\u901a\u77e5\u3002<\/p>\n<p>5.\u81ea\u52a8\u5316\u793e\u4ea4\u5a92\u4f53\u5e16\u5b50<\/p>\n<p>\u4f7f\u7528 python \u5b89\u6392\u793e\u4ea4\u5a92\u4f53\u5e16\u5b50\u53d8\u5f97\u8f7b\u800c\u6613\u4e3e\u3002\u4f7f\u7528\u201ctweepy\u201d\u5e93\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u53d1\u5e03\u63a8\u6587\u3002<\/p>\n<pre>import tweepy\n\ndef post_tweet(api_key, api_key_secret, access_token, access_token_secret, tweet):\n    auth = tweepy.oauthhandler(api_key, api_key_secret)\n    auth.set_access_token(access_token, access_token_secret)\n    api = tweepy.api(auth)\n    api.update_status(tweet)\n    print(\"tweet posted!\")\n\n# example usage\npost_tweet(\"api_key\", \"api_key_secret\", \"access_token\", \"access_token_secret\", \"hello, twitter!\")\n\n<\/pre>\n<p>\u975e\u5e38\u9002\u5408\u60f3\u8981\u63d0\u524d\u8ba1\u5212\u5e16\u5b50\u7684\u793e\u4ea4\u5a92\u4f53\u7ba1\u7406\u8005\u548c\u5185\u5bb9\u521b\u5efa\u8005\u3002<\/p>\n<p>6.pdf \u5230\u6587\u672c\u8f6c\u6362<\/p>\n<p>\u624b\u52a8\u4ece pdf \u4e2d\u63d0\u53d6\u6587\u672c\u975e\u5e38\u7e41\u7410\u3002\u8be5\u811a\u672c\u4f7f\u7528\u201cpypdf2\u201d\u5e93\u7b80\u5316\u4e86\u6d41\u7a0b\u3002<\/p>\n<pre>from pypdf2 import pdfreader\n\ndef pdf_to_text(file_path):\n    reader = pdfreader(file_path)\n    text = \"\"\n    for page in reader.pages:\n        text += page.extract_text()\n    return text\n\n# example usage\nprint(pdf_to_text(\"sample.pdf\"))\n\n<\/pre>\n<p>\u975e\u5e38\u9002\u5408\u5f52\u6863\u6216\u5206\u6790\u6587\u672c\u8f83\u591a\u7684\u6587\u6863\u3002<\/p>\n<p>7.\u4f7f\u7528 csv \u8fdb\u884c\u8d39\u7528\u8ddf\u8e2a<\/p>\n<p>\u901a\u8fc7\u5c06\u8d39\u7528\u8bb0\u5f55\u5230 csv \u6587\u4ef6\u4e2d\u6765\u8ddf\u8e2a\u60a8\u7684\u8d39\u7528\u3002\u6b64\u811a\u672c\u53ef\u5e2e\u52a9\u60a8\u7ef4\u62a4\u6570\u5b57\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u7a0d\u540e\u5206\u6790\u3002<\/p>\n<pre>import csv\n\ndef log_expense(file_name, date, item, amount):\n    with open(file_name, mode='a', newline='') as file:\n        writer = csv.writer(file)\n        writer.writerow([date, item, amount])\n        print(\"expense logged!\")\n\n# example usage\nlog_expense(\"expenses.csv\", \"2024-11-22\", \"coffee\", 4.5)\n\n<\/pre>\n<p>\u5c06\u5176\u53d8\u6210\u4e00\u79cd\u4e60\u60ef\uff0c\u60a8\u5c31\u4f1a\u6e05\u695a\u5730\u4e86\u89e3\u81ea\u5df1\u7684\u6d88\u8d39\u6a21\u5f0f\u3002<\/p>\n<p>8.\u81ea\u52a8\u5316\u684c\u9762\u901a\u77e5<\/p>\n<p>\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u9700\u8981\u63d0\u9192\u6216\u8b66\u62a5\u5417\uff1f\u8be5\u811a\u672c\u4f7f\u7528\u201cplyer\u201d\u5e93\u53d1\u9001\u684c\u9762\u901a\u77e5\u3002<\/p>\n<pre>from plyer import notification\n\ndef send_notification(title, message):\n    notification.notify(\n        title=title,\n        message=message,\n        app_name=\"task automation\"\n    )\n\n# example usage\nsend_notification(\"reminder\", \"meeting at 3 pm.\")\n\n<\/pre>\n<p>\u975e\u5e38\u9002\u5408\u4efb\u52a1\u7ba1\u7406\u548c\u4e8b\u4ef6\u63d0\u9192\u3002<\/p>\n<p>9.\u7f51\u7ad9\u53ef\u7528\u6027\u68c0\u67e5<\/p>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u7b80\u5355\u7684\u811a\u672c\u76d1\u63a7\u60a8\u7684\u7f51\u7ad9\u6216\u559c\u7231\u7684\u5e73\u53f0\u7684\u6b63\u5e38\u8fd0\u884c\u65f6\u95f4\u3002<\/p>\n<pre>import requests\n\ndef check_website(url):\n    try:\n        response = requests.get(url)\n        if response.status_code == 200:\n            print(f\"{url} is online!\")\n        else:\n            print(f\"{url} is down! status code: {response.status_code}\")\n    except exception as e:\n        print(f\"error: {e}\")\n\n# example usage\ncheck_website(\"https:\/\/www.google.com\")\n\n<\/pre>\n<p>\u5bf9\u7f51\u7edc\u5f00\u53d1\u4eba\u5458\u548c\u4f01\u4e1a\u4e3b\u6709\u7528\u3002<\/p>\n<p>10.\u81ea\u52a8\u5316\u6570\u636e\u5907\u4efd<\/p>\n<p>\u518d\u4e5f\u4e0d\u7528\u62c5\u5fc3\u4e22\u5931\u91cd\u8981\u6587\u4ef6\u4e86\u3002\u8be5\u811a\u672c\u81ea\u52a8\u5c06\u6587\u4ef6\u5907\u4efd\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002<\/p>\n<pre>import shutil\n\ndef backup_files(source_folder, backup_folder):\n    shutil.copytree(source_folder, backup_folder, dirs_exist_ok=True)\n    print(\"Backup completed!\")\n\n# Example usage\nbackup_files(\"C:\/ImportantData\", \"D:\/Backup\")\n\n<\/pre>\n<p>\u6bcf\u5468\u6216\u6bcf\u5929\u8fd0\u884c\u4e00\u6b21\uff0c\u4ee5\u786e\u4fdd\u60a8\u7684\u6570\u636e\u59cb\u7ec8\u5b89\u5168\u3002<\/p>\n<hr>\n<p>\u8fd9 10 \u4e2a\u811a\u672c\u6f14\u793a\u4e86 python \u5982\u4f55\u5904\u7406\u91cd\u590d\u6027\u4efb\u52a1\u5e76\u7b80\u5316\u60a8\u7684\u65e5\u5e38\u751f\u6d3b\u3002\u4ece\u7ba1\u7406\u6587\u4ef6\u5230\u5728\u793e\u4ea4\u5a92\u4f53\u4e0a\u53d1\u5e03\uff0c\u81ea\u52a8\u5316\u5f00\u542f\u4e86\u65e0\u9650\u7684\u53ef\u80fd\u6027\u3002\u9009\u62e9\u4e00\u4e2a\u811a\u672c\uff0c\u5bf9\u5176\u8fdb\u884c\u81ea\u5b9a\u4e49\uff0c\u5e76\u5c06\u5176\u96c6\u6210\u5230\u60a8\u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\u3002\u5f88\u5feb\uff0c\u60a8\u5c31\u4f1a\u60f3\u77e5\u9053\u5982\u679c\u6ca1\u6709 python \u81ea\u52a8\u5316\uff0c\u60a8\u662f\u5982\u4f55\u751f\u6d3b\u7684\uff01<\/p>\n<p>\u4f60\u4f1a\u5148\u5c1d\u8bd5\u54ea\u4e00\u4e2a\uff1f <\/p>\n<p>\u8bf7\u5728\u8bc4\u8bba\u90e8\u5206\u544a\u8bc9\u6211\u4eec\uff01<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\u7684 Python \u811a\u672c\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\u7684 Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205177","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205177"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205177\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}