{"id":97006,"date":"2024-10-20T14:12:17","date_gmt":"2024-10-20T06:12:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97006\/"},"modified":"2024-10-20T14:12:18","modified_gmt":"2024-10-20T06:12:18","slug":"python%e6%8a%80%e8%a1%93%e5%af%a6%e7%8f%be%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%bf%a1%e6%81%af%e7%8d%b2%e5%8f%96-python%e7%88%ac%e5%8f%96%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97006\/","title":{"rendered":"Python\u6280\u8853\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u4fe1\u606f\u7372\u53d6 (python\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e6%8a%80%e8%a1%93%e5%af%a6%e7%8f%be%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%bf%a1%e6%81%af%e7%8d%b2%e5%8f%96-python%e7%88%ac%e5%8f%96%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a-MDtKpZJiYb\">Python\u6280\u8853\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u4fe1\u606f\u7372\u53d6 (python\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u5eab)<\/h1>\n<p>\u96a8\u8457\u793e\u4ea4\u5a92\u9ad4\u7684\u666e\u53ca\uff0c\u5fae\u4fe1\u4f5c\u70ba\u4e2d\u570b\u6700\u53d7\u6b61\u8fce\u7684\u5373\u6642\u901a\u8a0a\u61c9\u7528\u4e4b\u4e00\uff0c\u64c1\u6709\u9f90\u5927\u7684\u7528\u6236\u57fa\u790e\u548c\u8c50\u5bcc\u7684\u6578\u64da\u8cc7\u6e90\u3002\u5c0d\u65bc\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u4f86\u8aaa\uff0c\u5f9e\u5fae\u4fe1\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u4fe1\u606f\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u6280\u8853\u4f86\u5be6\u73fe\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u4fe1\u606f\u7372\u53d6\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-MDtKpZJiYb\">\u4ec0\u9ebc\u662f\u5fae\u4fe1\u6578\u64da\u5eab\uff1f<\/h2>\n<p>\u5fae\u4fe1\u6578\u64da\u5eab\u662f\u6307\u5b58\u5132\u5728\u5fae\u4fe1\u5e73\u53f0\u4e0a\u7684\u5404\u985e\u6578\u64da\uff0c\u5305\u62ec\u7528\u6236\u4fe1\u606f\u3001\u804a\u5929\u8a18\u9304\u3001\u670b\u53cb\u5708\u52d5\u614b\u7b49\u3002\u9019\u4e9b\u6578\u64da\u5c0d\u65bc\u5e02\u5834\u5206\u6790\u3001\u7528\u6236\u884c\u70ba\u7814\u7a76\u7b49\u65b9\u9762\u5177\u6709\u91cd\u8981\u50f9\u503c\u3002\u7136\u800c\uff0c\u7531\u65bc\u5fae\u4fe1\u7684\u6578\u64da\u4fdd\u8b77\u653f\u7b56\uff0c\u76f4\u63a5\u8a2a\u554f\u9019\u4e9b\u6578\u64da\u4e26\u4e0d\u5bb9\u6613\u3002<\/p>\n<h2 id=\"%e7%88%ac%e5%8f%96%e5%be%ae%e4%bf%a1%e6%95%b8%e6%93%9a%e7%9a%84%e6%b3%95%e5%be%8b%e8%88%87%e5%80%ab%e7%90%86%e8%80%83%e9%87%8f-MDtKpZJiYb\">\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u7684\u6cd5\u5f8b\u8207\u502b\u7406\u8003\u91cf<\/h2>\n<p>\u5728\u958b\u59cb\u722c\u53d6\u6578\u64da\u4e4b\u524d\uff0c\u958b\u767c\u8005\u5fc5\u9808\u4e86\u89e3\u76f8\u95dc\u7684\u6cd5\u5f8b\u548c\u502b\u7406\u554f\u984c\u3002\u6839\u64da\u300a\u500b\u4eba\u4fe1\u606f\u4fdd\u8b77\u6cd5\u300b\uff0c\u672a\u7d93\u7528\u6236\u540c\u610f\u6536\u96c6\u5176\u500b\u4eba\u6578\u64da\u662f\u9055\u6cd5\u7684\u3002\u56e0\u6b64\uff0c\u5728\u9032\u884c\u6578\u64da\u722c\u53d6\u6642\uff0c\u61c9\u78ba\u4fdd\u9075\u5faa\u6cd5\u5f8b\u898f\u7bc4\uff0c\u4e26\u5c0a\u91cd\u7528\u6236\u7684\u96b1\u79c1\u6b0a\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8python%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e7%88%ac%e5%8f%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-MDtKpZJiYb\">\u4f7f\u7528Python\u9032\u884c\u6578\u64da\u722c\u53d6\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li><strong>\u74b0\u5883\u6e96\u5099\uff1a<\/strong>\u78ba\u4fdd\u5df2\u5b89\u88ddPython\u53ca\u76f8\u95dc\u5eab\uff0c\u5982requests\u548cBeautifulSoup\u3002<\/li>\n<li><strong>\u7372\u53d6\u76ee\u6a19URL\uff1a<\/strong>\u78ba\u5b9a\u8981\u722c\u53d6\u7684\u5fae\u4fe1\u9801\u9762URL\u3002<\/li>\n<li><strong>\u767c\u9001\u8acb\u6c42\uff1a<\/strong>\u4f7f\u7528requests\u5eab\u767c\u9001HTTP\u8acb\u6c42\u4ee5\u7372\u53d6\u9801\u9762\u5167\u5bb9\u3002<\/li>\n<li><strong>\u89e3\u6790\u6578\u64da\uff1a<\/strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5167\u5bb9\uff0c\u63d0\u53d6\u6240\u9700\u6578\u64da\u3002<\/li>\n<li><strong>\u6578\u64da\u5b58\u5132\uff1a<\/strong>\u5c07\u63d0\u53d6\u7684\u6578\u64da\u5b58\u5132\u5230\u672c\u5730\u6587\u4ef6\u6216\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-MDtKpZJiYb\">\u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u5fae\u4fe1\u516c\u773e\u865f\u7684\u6587\u7ae0\u6a19\u984c\uff1a<\/p>\n<pre><code>import requests\nfrom bs4 import BeautifulSoup\n\n# \u8a2d\u5b9a\u76ee\u6a19URL\nurl = 'https:\/\/mp.weixin.qq.com\/s\/xxxxxxxxxx'\n\n# \u767c\u9001\u8acb\u6c42\nresponse = requests.get(url)\nresponse.encoding = 'utf-8'\n\n# \u89e3\u6790HTML\nsoup = BeautifulSoup(response.text, 'html.parser')\n\n# \u63d0\u53d6\u6587\u7ae0\u6a19\u984c\ntitle = soup.find('h2', class_='rich_media_title').text.strip()\nprint('\u6587\u7ae0\u6a19\u984c:', title)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5c0e\u5165\u4e86requests\u548cBeautifulSoup\u5eab\uff0c\u7136\u5f8c\u8a2d\u7f6e\u4e86\u76ee\u6a19URL\u3002\u63a5\u8457\uff0c\u6211\u5011\u767c\u9001HTTP\u8acb\u6c42\u4e26\u89e3\u6790\u8fd4\u56de\u7684HTML\u5167\u5bb9\uff0c\u6700\u7d42\u63d0\u53d6\u51fa\u6587\u7ae0\u7684\u6a19\u984c\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2-MDtKpZJiYb\">\u6578\u64da\u5b58\u5132<\/h2>\n<p>\u63d0\u53d6\u5230\u7684\u6578\u64da\u53ef\u4ee5\u5b58\u5132\u5728\u591a\u7a2e\u683c\u5f0f\u4e2d\uff0c\u4f8b\u5982CSV\u3001JSON\u6216\u76f4\u63a5\u5b58\u5165\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u5b58\u5132\u70baCSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import csv\n\n# \u5047\u8a2d\u6211\u5011\u5df2\u7d93\u63d0\u53d6\u4e86\u591a\u500b\u6a19\u984c\ntitles = ['\u6a19\u984c1', '\u6a19\u984c2', '\u6a19\u984c3']\n\n# \u5beb\u5165CSV\u6587\u4ef6\nwith open('weixin_titles.csv', mode='w', newline='', encoding='utf-8') as file:\n    writer = csv.writer(file)\n    writer.writerow(['\u6a19\u984c'])\n    for title in titles:\n        writer.writerow([title])\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MDtKpZJiYb\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u6280\u8853\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u5eab\u7684\u4fe1\u606f\u7372\u53d6\u662f\u4e00\u500b\u6709\u8da3\u4e14\u5177\u6311\u6230\u6027\u7684\u4efb\u52d9\u3002\u96d6\u7136\u6280\u8853\u4e0a\u53ef\u884c\uff0c\u4f46\u958b\u767c\u8005\u5fc5\u9808\u59cb\u7d42\u9075\u5faa\u6cd5\u5f8b\u548c\u502b\u7406\u898f\u7bc4\uff0c\u4ee5\u4fdd\u8b77\u7528\u6236\u7684\u96b1\u79c1\u6b0a\u3002\u900f\u904e\u9069\u7576\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u53d6\u548c\u5206\u6790\u5fae\u4fe1\u6578\u64da\uff0c\u5f9e\u800c\u7372\u5f97\u6709\u50f9\u503c\u7684\u898b\u89e3\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u60a8\u7684Python\u722c\u87f2\uff0c\u8003\u616e\u4f7f\u7528\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u7684\u4f3a\u670d\u5668\u652f\u6301\uff0c\u52a9\u60a8\u8f15\u9b06\u5b8c\u6210\u6578\u64da\u722c\u53d6\u4efb\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u6280\u8853\u722c\u53d6\u5fae\u4fe1\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u4fe1\u606f\u7372\u53d6\uff0c\u52a9\u529b\u6578\u64da\u5206\u6790\u8207\u61c9\u7528\u958b\u767c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-97006","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97006","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=97006"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97006\/revisions"}],"predecessor-version":[{"id":97007,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97006\/revisions\/97007"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}