{"id":97012,"date":"2024-10-20T14:15:17","date_gmt":"2024-10-20T06:15:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97012\/"},"modified":"2024-10-20T14:15:18","modified_gmt":"2024-10-20T06:15:18","slug":"python%e7%88%ac%e8%9f%b2%e5%8a%a9%e4%bd%a0%e7%8d%b2%e5%8f%96%e5%ae%8c%e6%95%b4%e5%bd%a9%e7%a5%a8%e6%95%b8%e6%93%9a%e5%ba%ab-python-%e7%88%ac%e8%9f%b2%e5%bd%a9%e7%a5%a8%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97012\/","title":{"rendered":"Python\u722c\u87f2\u52a9\u4f60\u7372\u53d6\u5b8c\u6574\u5f69\u7968\u6578\u64da\u5eab (python \u722c\u87f2\u5f69\u7968\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python%e7%88%ac%e8%9f%b2%e5%8a%a9%e4%bd%a0%e7%8d%b2%e5%8f%96%e5%ae%8c%e6%95%b4%e5%bd%a9%e7%a5%a8%e6%95%b8%e6%93%9a%e5%ba%ab-ZglamolUpL\">Python\u722c\u87f2\u52a9\u4f60\u7372\u53d6\u5b8c\u6574\u5f69\u7968\u6578\u64da\u5eab<\/h1>\n<p>\u96a8\u8457\u6578\u64da\u79d1\u5b78\u7684\u8208\u8d77\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u4eba\u958b\u59cb\u5229\u7528Python\u9032\u884c\u6578\u64da\u722c\u53d6\u3002\u5f69\u7968\u6578\u64da\u4f5c\u70ba\u4e00\u500b\u71b1\u9580\u7684\u6578\u64da\u4f86\u6e90\uff0c\u5438\u5f15\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u7684\u6ce8\u610f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Python\u722c\u87f2\u6280\u8853\u4f86\u7372\u53d6\u5b8c\u6574\u7684\u5f69\u7968\u6578\u64da\u5eab\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%afpython%e7%88%ac%e8%9f%b2%ef%bc%9f-ZglamolUpL\">\u4ec0\u9ebc\u662fPython\u722c\u87f2\uff1f<\/h2>\n<p>Python\u722c\u87f2\u662f\u4e00\u7a2e\u81ea\u52d5\u5316\u7a0b\u5e8f\uff0c\u80fd\u5920\u5f9e\u4e92\u806f\u7db2\u4e0a\u63d0\u53d6\u6578\u64da\u3002\u5b83\u901a\u904e\u6a21\u64ec\u4eba\u985e\u7528\u6236\u7684\u884c\u70ba\uff0c\u8a2a\u554f\u7db2\u9801\u4e26\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002Python\u64c1\u6709\u591a\u500b\u5f37\u5927\u7684\u5eab\uff0c\u5982BeautifulSoup\u3001Scrapy\u548cRequests\uff0c\u9019\u4e9b\u5eab\u4f7f\u5f97\u722c\u53d6\u6578\u64da\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u9ad8\u6548\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e8%a6%81%e7%88%ac%e5%8f%96%e5%bd%a9%e7%a5%a8%e6%95%b8%e6%93%9a%ef%bc%9f-ZglamolUpL\">\u70ba\u4ec0\u9ebc\u8981\u722c\u53d6\u5f69\u7968\u6578\u64da\uff1f<\/h2>\n<p>\u5f69\u7968\u6578\u64da\u7684\u722c\u53d6\u53ef\u4ee5\u5e6b\u52a9\u7528\u6236\u9032\u884c\u6578\u64da\u5206\u6790\u3001\u9810\u6e2c\u548c\u8da8\u52e2\u7814\u7a76\u3002\u901a\u904e\u5206\u6790\u6b77\u53f2\u6578\u64da\uff0c\u7528\u6236\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5f69\u7968\u7684\u8d70\u52e2\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e2d\u734e\u7684\u6a5f\u6703\u3002\u6b64\u5916\uff0c\u9019\u4e9b\u6578\u64da\u9084\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u5f69\u7968\u76f8\u95dc\u7684\u61c9\u7528\u7a0b\u5e8f\u6216\u670d\u52d9\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%96%8b%e5%a7%8b%e7%88%ac%e5%8f%96%e5%bd%a9%e7%a5%a8%e6%95%b8%e6%93%9a-ZglamolUpL\">\u5982\u4f55\u958b\u59cb\u722c\u53d6\u5f69\u7968\u6578\u64da<\/h2>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%80%ef%bc%9a%e9%81%b8%e6%93%87%e7%9b%ae%e6%a8%99%e7%b6%b2%e7%ab%99-ZglamolUpL\">\u6b65\u9a5f\u4e00\uff1a\u9078\u64c7\u76ee\u6a19\u7db2\u7ad9<\/h3>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u9078\u64c7\u4e00\u500b\u63d0\u4f9b\u5f69\u7968\u6578\u64da\u7684\u7db2\u7ad9\u3002\u8a31\u591a\u7db2\u7ad9\u6703\u63d0\u4f9b\u6b77\u53f2\u5f69\u7968\u6578\u64da\u7684\u67e5\u8a62\u529f\u80fd\uff0c\u9019\u4e9b\u6578\u64da\u901a\u5e38\u4ee5\u8868\u683c\u7684\u5f62\u5f0f\u5448\u73fe\u3002<\/p>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%8c%ef%bc%9a%e5%ae%89%e8%a3%9d%e5%bf%85%e8%a6%81%e7%9a%84%e5%ba%ab-ZglamolUpL\">\u6b65\u9a5f\u4e8c\uff1a\u5b89\u88dd\u5fc5\u8981\u7684\u5eab<\/h3>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0bPython\u5eab\uff1a<\/p>\n<pre><code>pip install requests beautifulsoup4<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%89%ef%bc%9a%e7%b7%a8%e5%af%ab%e7%88%ac%e8%9f%b2%e4%bb%a3%e7%a2%bc-ZglamolUpL\">\u6b65\u9a5f\u4e09\uff1a\u7de8\u5beb\u722c\u87f2\u4ee3\u78bc<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u722c\u87f2\u793a\u4f8b\uff0c\u8a72\u793a\u4f8b\u5c07\u5f9e\u4e00\u500b\u5047\u8a2d\u7684\u5f69\u7968\u7db2\u7ad9\u4e0a\u63d0\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>import requests\nfrom bs4 import BeautifulSoup\n\n# \u8a2d\u5b9a\u76ee\u6a19\u7db2\u5740\nurl = 'http:\/\/example.com\/lottodata'\n\n# \u767c\u9001\u8acb\u6c42\nresponse = requests.get(url)\n\n# \u89e3\u6790HTML\nsoup = BeautifulSoup(response.text, 'html.parser')\n\n# \u63d0\u53d6\u6578\u64da\nfor row in soup.find_all('tr'):\n    columns = row.find_all('td')\n    if columns:\n        date = columns[0].text\n        winning_numbers = columns[1].text\n        print(f'\u65e5\u671f: {date}, \u4e2d\u734e\u865f\u78bc: {winning_numbers}')\n<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f%e5%9b%9b%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2-ZglamolUpL\">\u6b65\u9a5f\u56db\uff1a\u6578\u64da\u5b58\u5132<\/h3>\n<p>\u63d0\u53d6\u5230\u7684\u6578\u64da\u53ef\u4ee5\u5b58\u5132\u5728CSV\u6587\u4ef6\u6216\u6578\u64da\u5eab\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u5b58\u5132\u5230CSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import csv\n\nwith open('lotto_data.csv', mode='w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow(['\u65e5\u671f', '\u4e2d\u734e\u865f\u78bc'])\n    for row in soup.find_all('tr'):\n        columns = row.find_all('td')\n        if columns:\n            date = columns[0].text\n            winning_numbers = columns[1].text\n            writer.writerow([date, winning_numbers])\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-ZglamolUpL\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u9032\u884c\u722c\u87f2\u6642\uff0c\u8acb\u9075\u5faa\u7db2\u7ad9\u7684\u4f7f\u7528\u689d\u6b3e\u548c\u722c\u87f2\u898f\u7bc4\u3002\u67d0\u4e9b\u7db2\u7ad9\u53ef\u80fd\u6703\u7981\u6b62\u722c\u87f2\u884c\u70ba\uff0c\u6216\u5c0d\u983b\u7e41\u8acb\u6c42\u8a2d\u7f6e\u9650\u5236\u3002\u6b64\u5916\uff0c\u70ba\u4e86\u907f\u514d\u5c0d\u7db2\u7ad9\u9020\u6210\u904e\u5927\u8ca0\u64d4\uff0c\u5efa\u8b70\u5728\u722c\u53d6\u6642\u8a2d\u7f6e\u9069\u7576\u7684\u5ef6\u9072\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZglamolUpL\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u722c\u87f2\u6280\u8853\u4f86\u7372\u53d6\u5b8c\u6574\u7684\u5f69\u7968\u6578\u64da\u5eab\u662f\u4e00\u500b\u6709\u8da3\u4e14\u5be6\u7528\u7684\u9805\u76ee\u3002\u901a\u904e\u5b78\u7fd2\u5982\u4f55\u7de8\u5beb\u722c\u87f2\uff0c\u4f60\u53ef\u4ee5\u7372\u5f97\u5927\u91cf\u7684\u6578\u64da\uff0c\u4e26\u9032\u884c\u6df1\u5165\u7684\u5206\u6790\u3002\u7121\u8ad6\u662f\u70ba\u4e86\u500b\u4eba\u8208\u8da3\u9084\u662f\u5c08\u696d\u9700\u6c42\uff0c\u638c\u63e1\u9019\u9805\u6280\u80fd\u90fd\u5c07\u5c0d\u4f60\u6709\u6240\u5e6b\u52a9\u3002\u5982\u679c\u4f60\u9700\u8981\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884c\u4f60\u7684\u722c\u87f2\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216\u5176\u4ed6\u96f2\u670d\u52d9\u4f86\u78ba\u4fdd\u4f60\u7684\u6578\u64da\u6293\u53d6\u904e\u7a0b\u9806\u5229\u9032\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u722c\u87f2\u6280\u8853\uff0c\u8f15\u9b06\u7372\u53d6\u5b8c\u6574\u7684\u5f69\u7968\u6578\u64da\u5eab\uff0c\u52a9\u4f60\u5206\u6790\u548c\u9810\u6e2c\u5f69\u7968\u8da8\u52e2\uff0c\u63d0\u5347\u4e2d\u734e\u6a5f\u6703\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-97012","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97012","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=97012"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97012\/revisions"}],"predecessor-version":[{"id":97013,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97012\/revisions\/97013"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}