{"id":5282,"date":"2025-02-19T18:50:03","date_gmt":"2025-02-19T10:50:03","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=5282"},"modified":"2025-02-19T19:16:18","modified_gmt":"2025-02-19T11:16:18","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84akshare%e5%ba%93%e8%af%bb%e5%8f%96%e8%82%a1%e7%a5%a8%e5%ae%9e%e6%97%b6%e6%95%b0%e6%8d%ae%e5%b9%b6%e4%bf%9d%e5%ad%98","status":"publish","type":"post","link":"http:\/\/nnqvlom6.ipyingshe.net:5347\/?p=5282","title":{"rendered":"\u4f7f\u7528python\u7684akshare\u5e93\u8bfb\u53d6\u80a1\u7968\u5b9e\u65f6\u6570\u636e\u5e76\u4fdd\u5b58"},"content":{"rendered":"\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e3b\u8981\u529f\u80fd\u662f\u5b9a\u65f6\u83b7\u53d6 A \u80a1\u80a1\u7968\u6570\u636e\u5e76\u4fdd\u5b58\u5230 Excel \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u5bfc\u5165 <code>akshare<\/code>\u3001<code>pandas<\/code>\u3001<code>os<\/code>\u3001<code>schedule<\/code>\u3001<code>time<\/code> \u548c <code>logging<\/code> \u5e93\uff0c\u7528\u4e8e\u83b7\u53d6\u80a1\u7968\u6570\u636e\u3001\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u65f6\u95f4\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u4e49\u83b7\u53d6\u5e76\u4fdd\u5b58\u80a1\u7968\u6570\u636e\u7684\u51fd\u6570<\/strong>\uff1a<code>get_and_save_stock_data<\/code> \u51fd\u6570\u4ece\u4e1c\u65b9\u8d22\u5bcc\u83b7\u53d6\u6700\u65b0\u7684 A \u80a1\u80a1\u7968\u6570\u636e\uff0c\u751f\u6210\u5e26\u6709\u65f6\u95f4\u6233\u7684\u6587\u4ef6\u540d\uff0c\u5c06\u6570\u636e\u4fdd\u5b58\u4e3a Excel \u6587\u4ef6\uff0c\u5e76\u8bb0\u5f55\u64cd\u4f5c\u6210\u529f\u6216\u5931\u8d25\u7684\u65e5\u5fd7\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u4f7f\u7528 <code>logging.basicConfig<\/code> \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55\uff0c\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3a <code>INFO<\/code>\uff0c\u5e76\u6307\u5b9a\u65e5\u5fd7\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/strong>\uff1a\u4f7f\u7528 <code>schedule<\/code> \u5e93\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff0c\u6bcf 10 \u5206\u949f\u8c03\u7528\u4e00\u6b21 <code>get_and_save_stock_data<\/code> \u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u5faa\u73af\u68c0\u67e5\u5e76\u6267\u884c\u5b9a\u65f6\u4efb\u52a1<\/strong>\uff1a\u4f7f\u7528 <code>while True<\/code> \u5faa\u73af\u4e0d\u65ad\u68c0\u67e5\u662f\u5426\u6709\u5b9a\u65f6\u4efb\u52a1\u5230\u671f\uff0c\u5e76\u6267\u884c\u5230\u671f\u7684\u4efb\u52a1\u3002\u6bcf\u6b21\u5faa\u73af\u6682\u505c 1 \u79d2\uff0c\u4ee5\u907f\u514d\u8fc7\u5ea6\u5360\u7528 CPU \u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4ee3\u7801\u5b9e\u73b0\u4e86\u5b9a\u65f6\u83b7\u53d6\u548c\u4fdd\u5b58\u80a1\u7968\u6570\u636e\u7684\u529f\u80fd\uff0c\u5e76\u8bb0\u5f55\u4e86\u64cd\u4f5c\u65e5\u5fd7\u3002<br>\u5728\u8fd9\u4e2a\u4fee\u6539\u540e\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>try-except<\/code> \u5757\u5305\u88f9\u4e86 <code>while<\/code> \u5faa\u73af\uff0c\u5f53\u7528\u6237\u6309\u4e0b <code>Ctrl+C<\/code> \u65f6\uff0c\u4f1a\u89e6\u53d1 <code>KeyboardInterrupt<\/code> \u5f02\u5e38\uff0c\u7a0b\u5e8f\u4f1a\u6355\u83b7\u8fd9\u4e2a\u5f02\u5e38\u5e76\u8bb0\u5f55\u4e00\u6761\u65e5\u5fd7\u4fe1\u606f\uff0c\u7136\u540e\u6b63\u5e38\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import akshare as ak\nimport pandas as pd\nimport os\nimport schedule\nimport time\nimport logging\n\ndef get_and_save_stock_data():\n        \"\"\"\n        Fetches the latest A-share stock data from Eastmoney and saves it to an Excel file.\n        The file is named with a timestamp and stored in the same directory as the script.\n        Logs the success or failure of the operation.\n        \"\"\"\n        try:\n            # \u83b7\u53d6\u80a1\u7968\u6570\u636e\n            data = ak.stock_zh_a_spot_em()\n            print(data.head())\n            # \u751f\u6210\u5e26\u65f6\u95f4\u7684\u6587\u4ef6\u540d\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 0.5 minutes\nschedule.every(0.5).minutes.do(get_and_save_stock_data)\n\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":"<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u4e3b\u8981\u529f\u80fd\u662f\u5b9a\u65f6\u83b7\u53d6 A \u80a1\u80a1\u7968\u6570\u636e\u5e76\u4fdd\u5b58\u5230 Excel \u6587\u4ef6\u4e2d\uff0c\u540c\u65f6\u8bb0 <span class=\"readmore\"><a href=\"http:\/\/nnqvlom6.ipyingshe.net:5347\/?p=5282\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,24],"tags":[],"class_list":["post-5282","post","type-post","status-publish","format-standard","hentry","category-python","category-24"],"_links":{"self":[{"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5282"}],"version-history":[{"count":3,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5282\/revisions"}],"predecessor-version":[{"id":5286,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5282\/revisions\/5286"}],"wp:attachment":[{"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5282"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nnqvlom6.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}