{"id":204156,"date":"2025-05-29T12:32:35","date_gmt":"2025-05-29T04:32:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204156\/"},"modified":"2025-05-29T12:32:35","modified_gmt":"2025-05-29T04:32:35","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e8%87%aa%e5%8a%a8%e5%8c%96%e6%97%a5%e5%b8%b8%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204156\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5\u5e38\u4efb\u52a1"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5\u5e38\u4efb\u52a1<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5\u5e38\u4efb\u52a1\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241030\/17302710776721d76573ab5.jpg\" class=\"aligncenter\"><\/p>\n<p><em>\u4f5c\u8005\uff1a\u7279\u91cc\u514b\u65af\u00b7\u8d5b\u52d2\u65af<\/em><\/p>\n<p>waymap\u6e17\u900f\u6d4b\u8bd5\u5de5\u5177\uff1a\u70b9\u51fb\u8fd9\u91cc<br \/> trixsec github\uff1a\u70b9\u51fb\u8fd9\u91cc<\/p>\n<p>python \u662f\u4e00\u79cd\u591a\u529f\u80fd\u4e14\u6613\u4e8e\u5b66\u4e60\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u6700\u5927\u7684\u4f18\u52bf\u4e4b\u4e00\u662f\u80fd\u591f\u81ea\u52a8\u6267\u884c\u91cd\u590d\u6027\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u7ec4\u7ec7\u6587\u4ef6\u3001\u6293\u53d6\u7f51\u7edc\u6570\u636e\u3001\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u8fd8\u662f\u7ba1\u7406\u7cfb\u7edf\u8d44\u6e90\uff0cpython \u7684\u5e93\u548c\u6a21\u5757\u90fd\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u65e5\u5e38\u4efb\u52a1\u7684\u6548\u7387\u3002<\/p>\n<p>\u5728\u672c\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u4f7f\u7528 python \u81ea\u52a8\u6267\u884c\u4efb\u52a1\u7684\u4e0d\u540c\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u6765\u5e2e\u52a9\u60a8\u5165\u95e8\u3002<\/p>\n<p><strong>1\u3002\u81ea\u52a8\u5316\u6587\u4ef6\u548c\u6587\u4ef6\u5939\u7ba1\u7406<\/strong><\/p>\n<p>python \u7684\u5185\u7f6e os \u548c shutil \u6a21\u5757\u5141\u8bb8\u60a8\u4e0e\u8ba1\u7b97\u673a\u7684\u6587\u4ef6\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u81ea\u52a8\u6267\u884c\u6587\u4ef6\u521b\u5efa\u3001\u5220\u9664\u548c\u7ec4\u7ec7\u4efb\u52a1\u3002<\/p>\n<pre>import os\nimport shutil\n\n# folder paths\nsource_folder = '\/path\/to\/source'\ndestination_folders = {\n    'images': '\/path\/to\/images',\n    'documents': '\/path\/to\/docs',\n    'music': '\/path\/to\/music'\n}\n\n# file extensions\nfile_types = {\n    '.jpg': 'images',\n    '.png': 'images',\n    '.pdf': 'documents',\n    '.mp3': 'music'\n}\n\n# move files to respective folders\nfor filename in os.listdir(source_folder):\n    ext = os.path.splitext(filename)[1].lower()\n    if ext in file_types:\n        shutil.move(os.path.join(source_folder, filename), destination_folders[file_types[ext]])\n<\/pre>\n<p>\u6b64\u811a\u672c\u626b\u63cf\u6e90\u6587\u4ef6\u5939\uff0c\u6839\u636e\u6269\u5c55\u540d\u8bc6\u522b\u6587\u4ef6\u7c7b\u578b\uff0c\u5e76\u5c06\u5b83\u4eec\u79fb\u52a8\u5230\u5404\u81ea\u7684\u6587\u4ef6\u5939\u3002<\/p>\n<p><strong>2\u3002\u81ea\u52a8\u7f51\u9875\u6293\u53d6<\/strong><br \/> \u7f51\u7edc\u6293\u53d6\u5141\u8bb8\u60a8\u81ea\u52a8\u4ece\u7f51\u7ad9\u6536\u96c6\u6570\u636e\u3002 requests \u548c beautifulsoup \u5e93\u901a\u5e38\u7528\u4e8e\u53d1\u9001 http \u8bf7\u6c42\u548c\u89e3\u6790 html \u5185\u5bb9\u3002<\/p>\n<pre>import requests\nfrom bs4 import beautifulsoup\n\n# url of the website to scrape\nurl = 'https:\/\/news.ycombinator.com\/'\n\n# send a get request\nresponse = requests.get(url)\nsoup = beautifulsoup(response.text, 'html.parser')\n\n# extract headlines\nheadlines = soup.find_all('a', class_='storylink')\n\n# print each headline\nfor headline in headlines:\n    print(headline.text)\n<\/pre>\n<p>\u6b64\u811a\u672c\u6293\u53d6\u5e76\u6253\u5370\u9ed1\u5ba2\u65b0\u95fb\u9996\u9875\u7684\u6240\u6709\u6807\u9898\u3002<\/p>\n<p><strong>3\u3002\u81ea\u52a8\u53d1\u9001\u7535\u5b50\u90ae\u4ef6<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 python \u7684 smtplib \u6a21\u5757\u81ea\u52a8\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u53d1\u9001\u63d0\u9192\u3001\u62a5\u544a\u6216\u901a\u77e5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre>import smtplib\nfrom email.mime.text import mimetext\n\n# email configuration\nsender_email = 'youremail@gmail.com'\nreceiver_email = 'recipient@gmail.com'\nsubject = 'daily reminder'\nbody = 'don\u2019t forget to complete your task today!'\n\n# create the email message\nmsg = mimetext(body)\nmsg['subject'] = subject\nmsg['from'] = sender_email\nmsg['to'] = receiver_email\n\n# send the email\nwith smtplib.smtp('smtp.gmail.com', 587) as server:\n    server.starttls()\n    server.login(sender_email, 'yourpassword')\n    server.sendmail(sender_email, receiver_email, msg.as_string())\n<\/pre>\n<p>\u6b64\u811a\u672c\u4f7f\u7528 gmail \u7684 smtp \u670d\u52a1\u5668\u53d1\u9001\u7b80\u5355\u7684\u63d0\u9192\u7535\u5b50\u90ae\u4ef6\u3002<\/p>\n<p><strong>4\u3002\u5728 google \u8868\u683c\u4e2d\u81ea\u52a8\u8f93\u5165\u6570\u636e<\/strong><\/p>\n<p>\u501f\u52a9 gspread \u5e93\u548c google sheets api\uff0c\u60a8\u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u5c06\u6570\u636e\u5199\u5165\u7535\u5b50\u8868\u683c\u7684\u8fc7\u7a0b\u3002<\/p>\n<pre>import gspread\nfrom oauth2client.service_account import serviceaccountcredentials\n\n# google sheets api setup\nscope = ['https:\/\/spreadsheets.google.com\/feeds', 'https:\/\/www.googleapis.com\/auth\/drive']\ncreds = serviceaccountcredentials.from_json_keyfile_name('credentials.json', scope)\nclient = gspread.authorize(creds)\n\n# open the sheet\nsheet = client.open('mysheet').sheet1\n\n# write data to the sheet\nsheet.update_cell(1, 1, 'task')\nsheet.update_cell(1, 2, 'status')\nsheet.update_cell(2, 1, 'complete python script')\nsheet.update_cell(2, 2, 'done')\n<\/pre>\n<p>\u6b64\u811a\u672c\u767b\u5f55 google sheets \u5e76\u5c06\u6570\u636e\u5199\u5165\u6307\u5b9a\u7684\u5de5\u4f5c\u8868\u3002<\/p>\n<p><strong>5\u3002\u81ea\u52a8\u5316\u7cfb\u7edf\u76d1\u63a7<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528python\u6765\u76d1\u63a7cpu\u3001\u5185\u5b58\u548c\u78c1\u76d8\u7b49\u7cfb\u7edf\u8d44\u6e90\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u8fd9\u5bf9\u4e8e\u670d\u52a1\u5668\u7ba1\u7406\u548c\u8bca\u65ad\u5f88\u6709\u7528\u3002<\/p>\n<pre>import psutil\n\n# get cpu and memory usage\ncpu_usage = psutil.cpu_percent(interval=1)\nmemory_info = psutil.virtual_memory()\n\nprint(f\"cpu usage: {cpu_usage}%\")\nprint(f\"memory usage: {memory_info.percent}%\")\n<\/pre>\n<p>\u6b64\u811a\u672c\u6253\u5370\u60a8\u673a\u5668\u5f53\u524d\u7684 cpu \u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<p><strong>6\u3002\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u4efb\u52a1<\/strong><\/p>\n<p>python \u7684 selenium \u5e93\u5141\u8bb8\u60a8\u901a\u8fc7\u811a\u672c\u63a7\u5236 web \u6d4f\u89c8\u5668\u3002\u8fd9\u5bf9\u4e8e\u586b\u5199\u8868\u5355\u3001\u6d4f\u89c8\u7f51\u7ad9\u548c\u6293\u53d6\u52a8\u6001\u5185\u5bb9\u7b49\u4efb\u52a1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre>from selenium import webdriver\nfrom selenium.webdriver.common.keys import Keys\n\n# Set up the browser\ndriver = webdriver.Chrome()\n\n# Open Google\ndriver.get('https:\/\/www.google.com')\n\n# Search for a query\nsearch_box = driver.find_element_by_name('q')\nsearch_box.send_keys('Python automation')\nsearch_box.send_keys(Keys.RETURN)\n<\/pre>\n<p>\u6b64\u811a\u672c\u6253\u5f00 google\uff0c\u641c\u7d22\u201cpython \u81ea\u52a8\u5316\u201d\uff0c\u5e76\u663e\u793a\u641c\u7d22\u7ed3\u679c\u3002<\/p>\n<p>~trixsec<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5\u5e38\u4efb\u52a1\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/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>\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u5316\u65e5&#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-204156","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204156","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=204156"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204156\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}