{"id":5593,"date":"2025-05-13T14:03:46","date_gmt":"2025-05-13T06:03:46","guid":{"rendered":"http:\/\/cnliutz.pgrm.cc\/?p=5593"},"modified":"2025-05-13T14:03:47","modified_gmt":"2025-05-13T06:03:47","slug":"python%e8%8e%b7%e5%8f%96%e6%9f%90%e5%8f%aa%e8%82%a1%e7%a5%a8%e7%9a%84%e5%8e%86%e5%8f%b2%e6%95%b0%e6%8d%ae%e5%92%8c%e6%89%80%e6%9c%89%e8%82%a1%e7%a5%a8%e6%9c%80%e6%96%b0%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5593","title":{"rendered":"python\u83b7\u53d6\u67d0\u53ea\u80a1\u7968\u7684\u5386\u53f2\u6570\u636e\u548c\u6240\u6709\u80a1\u7968\u6700\u65b0\u6570\u636e"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#\u83b7\u53d6\u6240\u6709A\u80a1\u80a1\u7968\u6700\u65b0\u6570\u636e\u5e76\u5199\u5165 Excel \u6587\u4ef6\nimport akshare as ak\nimport pandas as pd\nimport os\nimport schedule\nimport time\nimport logging\n\n# \u83b7\u53d6\u67d0\u53ea\u80a1\u7968\u5386\u53f2\u6570\u636e\u5e76\u5199\u5165 Excel \u6587\u4ef6\n\nstock_data = ak.stock_zh_a_hist(\n    symbol=\"000937\",\n    start_date=\"20220101\",\n    end_date=\"20250512\",\n    # \u8bbe\u7f6e\u590d\u6743\u7c7b\u578b\uff0c\u53ef\u9009\u524d\u590d\u6743\uff08qfq\uff09\u3001\u540e\u590d\u6743\uff08hfq\uff09\u6216\u4e0d\u590d\u6743\uff0c\u6b64\u5904\u6ce8\u91ca\u6389\u8868\u793a\u6682\u4e0d\u8bbe\u7f6e\n    # adjust=\"qfq\"  # \u53ef\u9009\u524d\u590d\u6743\u3001\u540e\u590d\u6743\u6216\u4e0d\u590d\u6743\n)\n\n# \u5b9a\u4e49\u8981\u5199\u5165\u6570\u636e\u7684 Excel \u6587\u4ef6\u8def\u5f84\nexcel_path = \"stock_data_000937.xlsx\"\n# \u5c06 Excel \u6587\u4ef6\u8def\u5f84\u4e0e\u5f53\u524d\u811a\u672c\u6240\u5728\u76ee\u5f55\u62fc\u63a5\uff0c\u786e\u4fdd\u6587\u4ef6\u4fdd\u5b58\u5728\u811a\u672c\u540c\u76ee\u5f55\u4e0b\nexcel_path = os.path.join(os.path.dirname(__file__), excel_path)\ntry:\n    # \u5c06\u83b7\u53d6\u5230\u7684\u80a1\u7968\u6570\u636e\u5199\u5165 Excel \u6587\u4ef6\uff0c\u4e0d\u5305\u542b\u7d22\u5f15\u5217\n    stock_data.to_excel(excel_path, index=False)\n    # \u6253\u5370\u6210\u529f\u4fe1\u606f\uff0c\u63d0\u793a\u6570\u636e\u5df2\u6210\u529f\u5199\u5165\u6307\u5b9a\u6587\u4ef6\n    print(f\"\u6570\u636e\u5df2\u6210\u529f\u5199\u5165\u5230 {excel_path} \u6587\u4ef6\u4e2d\uff01\")\nexcept Exception as e:\n    # \u82e5\u5199\u5165\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c\u6253\u5370\u9519\u8bef\u4fe1\u606f\n    print(f\"\u5199\u5165 Excel \u6587\u4ef6\u65f6\u51fa\u9519: {e}\")\n\n#\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5e76\u4fdd\u5b58\u80a1\u7968\u6700\u65b0\u80a1\u4ef7\u6570\u636e\ndef get_and_save_stock_data():\n        \"\"\"\n        \u4ece\u4e1c\u65b9\u8d22\u5bcc\u83b7\u53d6\u6700\u65b0\u7684 A \u80a1\u80a1\u7968\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u4e00\u4e2a Excel \u6587\u4ef6\u4e2d\u3002\n        \u6587\u4ef6\u540d\u5305\u542b\u65f6\u95f4\u6233\uff0c\u4e14\u6587\u4ef6\u4f1a\u5b58\u50a8\u5728\u811a\u672c\u6240\u5728\u7684\u540c\u4e00\u76ee\u5f55\u4e0b\u3002\n        \u8bb0\u5f55\u64cd\u4f5c\u6210\u529f\u6216\u5931\u8d25\u7684\u65e5\u5fd7\u4fe1\u606f\u3002\n        \"\"\"\n        try:\n            # \u8c03\u7528 akshare \u5e93\u7684\u51fd\u6570\u83b7\u53d6 A \u80a1\u5b9e\u65f6\u884c\u60c5\u6570\u636e\n            data = ak.stock_zh_a_spot_em()\n            # \u6253\u5370\u83b7\u53d6\u5230\u7684\u6570\u636e\u7684\u524d\u51e0\u884c\uff0c\u7528\u4e8e\u8c03\u8bd5\u67e5\u770b\u6570\u636e\u7ed3\u6784\n            print(data.head())\n            # \u4f7f\u7528 time.strftime \u51fd\u6570\u751f\u6210\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\uff0c\u683c\u5f0f\u4e3a\u5e74\u6708\u65e5\u65f6\u5206\u79d2\n            current_time = time.strftime(\"%Y%m%d%H%M%S\")\n            # Generate a file name with a timestamp\n            file_name = f'stock_data_{current_time}.xlsx'\n            # Create a full file path by combining the directory of the current script and the file name\n            file_path = os.path.join(os.path.dirname(__file__), file_name)\n            # Write the data to an Excel file, without including the index\n            data.to_excel(file_path, index=False)\n            # Log the successful retrieval and storage of the stock data\n            logging.info(f\"Successfully retrieved stock data and saved to {file_path} at {time.strftime('%Y-%m-%d %H:%M:%S')}\")\n        except Exception as e:\n            # Log any errors that occur during the retrieval or storage of the stock data\n            logging.error(f\"An error occurred while retrieving or saving stock data: {e}\")\n\n# Configure logging to record events at the INFO level\nlogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n\n# Schedule the function to run every 10 or\uff080.5\uff09 minutes\nschedule.every(1).minutes.do(get_and_save_stock_data)\n\n# Start the scheduling loop\uff0cCtrl+C to stop \u7ec8\u6b62\u8fd0\u884c\ntry:\n    # Continuously check and run scheduled tasks\n    while True:\n        # Check if any scheduled tasks are due and run them\n        schedule.run_pending()\n        # Pause the loop for 1 second to avoid excessive CPU usage\n        time.sleep(1)\nexcept KeyboardInterrupt:\n    logging.info(\"Program terminated by user.\u7ed3\u675f\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5593","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5593"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5593\/revisions"}],"predecessor-version":[{"id":5594,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5593\/revisions\/5594"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5593"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}