{"id":204234,"date":"2025-05-29T13:42:01","date_gmt":"2025-05-29T05:42:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204234\/"},"modified":"2025-05-29T13:42:01","modified_gmt":"2025-05-29T05:42:01","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-python-%e8%87%aa%e5%8a%a8%e6%89%a7%e8%a1%8c%e6%97%a5%e5%b8%b8%e4%bb%bb%e5%8a%a1%ef%bc%88%e7%ac%ac-2-%e9%83%a8%e5%88%86%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204234\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\uff08\u7b2c 2 \u90e8\u5206\uff09"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\uff08\u7b2c 2 \u90e8\u5206\uff09<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\uff08\u7b2c 2 \u90e8\u5206\uff09\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304698066724dfae95c50.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>\u5728\u7b2c 1 \u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u63a2\u7d22\u4e86\u5982\u4f55\u4f7f\u7528 python \u6765\u81ea\u52a8\u5316\u6587\u4ef6\u7ba1\u7406\u3001\u7f51\u9875\u6293\u53d6\u3001\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u3001google \u8868\u683c\u548c\u7cfb\u7edf\u76d1\u63a7\u3002\u5728\u7b2c 2 \u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u4ecb\u7ecd\u66f4\u9ad8\u7ea7\u7684\u4efb\u52a1\uff0c\u4f8b\u5982\u81ea\u52a8\u5316 api\u3001\u8c03\u5ea6\u811a\u672c\u4ee5\u53ca\u5c06\u81ea\u52a8\u5316\u4e0e\u7b2c\u4e09\u65b9\u670d\u52a1\u96c6\u6210\u3002<\/p>\n<p><strong>7\u3002\u81ea\u52a8\u5316 api \u8bf7\u6c42<\/strong><\/p>\n<p>\u8bb8\u591a web \u670d\u52a1\u63d0\u4f9b api \u6765\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u4e0e\u5176\u5e73\u53f0\u8fdb\u884c\u4ea4\u4e92\u3002\u4f7f\u7528\u8bf7\u6c42\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u81ea\u52a8\u6267\u884c\u4efb\u52a1\uff0c\u4f8b\u5982\u4ece api \u83b7\u53d6\u6570\u636e\u3001\u53d1\u5e03\u66f4\u65b0\u6216\u5728\u4e91\u670d\u52a1\u4e0a\u6267\u884c crud \u64cd\u4f5c\u3002<\/p>\n<pre>import requests\n\n# openweathermap api configuration\napi_key = 'your_api_key'\ncity = 'new york'\nurl = f'http:\/\/api.openweathermap.org\/data\/2.5\/weather?q={city}&amp;appid={api_key}'\n\n# send a get request to fetch weather data\nresponse = requests.get(url)\ndata = response.json()\n\n# extract temperature information\ntemperature = data['main']['temp']\nweather = data['weather'][0]['description']\n\nprint(f\"temperature: {temperature}\u00b0k\")\nprint(f\"weather: {weather}\")\n<\/pre>\n<p>\u6b64\u811a\u672c\u4ece openweathermap api \u83b7\u53d6\u6307\u5b9a\u57ce\u5e02\u7684\u5f53\u524d\u5929\u6c14\u6570\u636e\u5e76\u663e\u793a\u5b83\u3002<\/p>\n<p><strong>8\u3002\u4f7f\u7528 python \u5b89\u6392\u4efb\u52a1<\/strong><\/p>\n<p>\u6709\u65f6\u60a8\u9700\u8981\u81ea\u52a8\u6267\u884c\u4efb\u52a1\u4ee5\u5728\u7279\u5b9a\u65f6\u95f4\u6216\u95f4\u9694\u8fd0\u884c\u3002 python \u7684\u8ba1\u5212\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u5728\u7279\u5b9a\u65f6\u95f4\u81ea\u52a8\u8fd0\u884c\u7684\u4f5c\u4e1a\u3002<\/p>\n<pre>import schedule\nimport time\n\n# task function to be executed\ndef task():\n    print(\"executing scheduled task...\")\n\n# schedule the task to run every day at 9 am\nschedule.every().day.at(\"09:00\").do(task)\n\n# keep the script running to check the schedule\nwhile true:\n    schedule.run_pending()\n    time.sleep(1)\n<\/pre>\n<p>\u6b64\u811a\u672c\u5b89\u6392\u4efb\u52a1\u5728\u6bcf\u5929\u4e0a\u5348 9 \u70b9\u8fd0\u884c\uff0c\u4f7f\u7528\u7b80\u5355\u7684\u8c03\u5ea6\u5faa\u73af\u6765\u4fdd\u6301\u4efb\u52a1\u8fd0\u884c\u3002<\/p>\n<p><strong>9\u3002\u81ea\u52a8\u5316\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/p>\n<p>python \u53ef\u7528\u4e8e\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3001\u81ea\u52a8\u8f93\u5165\u6570\u636e\u4ee5\u53ca\u6267\u884c\u8bfb\u53d6\u3001\u66f4\u65b0\u548c\u5220\u9664\u8bb0\u5f55\u7b49\u64cd\u4f5c\u3002 sqlite3 \u6a21\u5757\u5141\u8bb8\u60a8\u7ba1\u7406 sqlite \u6570\u636e\u5e93\uff0c\u800c\u5176\u4ed6\u5e93\uff08\u5982 psycopg2 \u6216 mysqldb\uff09\u53ef\u4e0e postgresql \u548c mysql \u914d\u5408\u4f7f\u7528\u3002<\/p>\n<pre>import sqlite3\n\n# connect to sqlite database\nconn = sqlite3.connect('tasks.db')\n\n# create a cursor object to execute sql commands\ncur = conn.cursor()\n\n# create a table for storing tasks\ncur.execute('''create table if not exists tasks (id integer primary key, task_name text, status text)''')\n\n# insert a new task\ncur.execute(\"insert into tasks (task_name, status) values ('complete automation script', 'pending')\")\n\n# commit changes and close the connection\nconn.commit()\nconn.close()\n<\/pre>\n<p>\u6b64\u811a\u672c\u521b\u5efa\u4e00\u4e2a sqlite \u6570\u636e\u5e93\uff0c\u6dfb\u52a0\u4e00\u4e2a\u201c\u4efb\u52a1\u201d\u8868\uff0c\u5e76\u5411\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u4efb\u52a1\u3002<\/p>\n<p><strong>10\u3002\u81ea\u52a8\u5316 excel \u6587\u4ef6\u7ba1\u7406<\/strong><\/p>\n<p>python \u4e0e openpyxl \u6216 pandas \u5e93\u4e00\u8d77\u53ef\u7528\u4e8e\u81ea\u52a8\u8bfb\u53d6\u3001\u5199\u5165\u548c\u4fee\u6539 excel \u6587\u4ef6\u3002\u8fd9\u5bf9\u4e8e\u81ea\u52a8\u5316\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u4efb\u52a1\u7279\u522b\u6709\u7528\u3002<\/p>\n<pre>import pandas as pd\n\n# read excel file\ndf = pd.read_excel('data.xlsx')\n\n# perform some operation on the data\ndf['total'] = df['price'] * df['quantity']\n\n# write the modified data back to a new excel file\ndf.to_excel('updated_data.xlsx', index=false)\n<\/pre>\n<p>\u6b64\u811a\u672c\u8bfb\u53d6 excel \u6587\u4ef6\uff0c\u5bf9\u6570\u636e\u6267\u884c\u8ba1\u7b97\uff0c\u5e76\u5c06\u66f4\u65b0\u7684\u6570\u636e\u5199\u5165\u65b0\u6587\u4ef6\u3002<\/p>\n<p><strong>11\u3002\u4f7f\u7528 selenium \u5b9e\u73b0\u6d4f\u89c8\u5668\u4ea4\u4e92\u81ea\u52a8\u5316<\/strong><\/p>\n<p>\u4f7f\u7528 selenium\uff0cpython \u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u4e0e web \u6d4f\u89c8\u5668\u7684\u4ea4\u4e92\uff0c\u4f8b\u5982\u767b\u5f55\u5e10\u6237\u3001\u586b\u5199\u8868\u5355\u548c\u6267\u884c\u91cd\u590d\u7684 web \u4efb\u52a1\u3002<\/p>\n<pre>from selenium import webdriver\nfrom selenium.webdriver.common.keys import keys\n\n# set up the browser driver\ndriver = webdriver.chrome()\n\n# open the login page\ndriver.get('https:\/\/example.com\/login')\n\n# locate the username and password fields, fill them in, and log in\nusername = driver.find_element_by_name('username')\npassword = driver.find_element_by_name('password')\nusername.send_keys('your_username')\npassword.send_keys('your_password')\npassword.send_keys(keys.return)\n\n# close the browser\ndriver.quit()\n<\/pre>\n<p>\u6b64\u811a\u672c\u6253\u5f00 web \u6d4f\u89c8\u5668\uff0c\u5bfc\u822a\u5230\u767b\u5f55\u9875\u9762\uff0c\u586b\u5199\u51ed\u636e\uff0c\u7136\u540e\u81ea\u52a8\u767b\u5f55\u3002<\/p>\n<p><strong>12\u3002\u81ea\u52a8\u5316\u4e91\u670d\u52a1<\/strong><\/p>\n<p>python \u4e0e aws\u3001google cloud \u548c azure \u7b49\u4e91\u670d\u52a1\u96c6\u6210\u826f\u597d\u3002\u4f7f\u7528 boto3 \u5e93\uff0c\u60a8\u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u7ba1\u7406 aws \u4e2d\u7684 s3 \u5b58\u50a8\u6876\u3001ec2 \u5b9e\u4f8b\u548c lambda \u51fd\u6570\u7b49\u4efb\u52a1\u3002<\/p>\n<pre>import boto3\n\n# connect to s3\ns3 = boto3.client('s3')\n\n# list all buckets\nbuckets = s3.list_buckets()\nfor bucket in buckets['buckets']:\n    print(bucket['name'])\n\n# create a new bucket\ns3.create_bucket(bucket='my-new-bucket')\n\n# upload a file to the bucket\ns3.upload_file('file.txt', 'my-new-bucket', 'file.txt')\n<\/pre>\n<p>\u6b64\u811a\u672c\u8fde\u63a5\u5230 aws s3\uff0c\u5217\u51fa\u6240\u6709\u5b58\u50a8\u6876\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u5b58\u50a8\u6876\uff0c\u5e76\u5411\u5176\u4e2d\u4e0a\u4f20\u6587\u4ef6\u3002<\/p>\n<p><strong>13\u3002\u81ea\u52a8\u5316 pdf \u64cd\u4f5c<\/strong><\/p>\n<p>\u4f7f\u7528 pypdf2 \u5e93\uff0cpython \u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u5408\u5e76\u3001\u62c6\u5206\u548c\u4ece pdf \u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u7b49\u4efb\u52a1\u3002<\/p>\n<pre>import PyPDF2\n\n# List of PDF files to merge\npdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf']\n\n# Create a PDF merger object\nmerger = PyPDF2.PdfMerger()\n\n# Loop through the PDFs and append them to the merger\nfor pdf in pdfs:\n    merger.append(pdf)\n\n# Write the merged PDF to a new file\nwith open('merged_file.pdf', 'wb') as f:\n    merger.write(f)\n<\/pre>\n<p>\u6b64\u811a\u672c\u5c06\u591a\u4e2a pdf \u6587\u4ef6\u5408\u5e76\u4e3a\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<p>~trixsec<\/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\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u6267\u884c\u65e5\u5e38\u4efb\u52a1\uff08\u7b2c 2 \u90e8\u5206\uff09\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>\u5982\u4f55\u4f7f\u7528 Python \u81ea\u52a8\u6267\u884c&#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-204234","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204234","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=204234"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204234\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}