{"id":551,"date":"2024-12-26T22:50:37","date_gmt":"2024-12-26T14:50:37","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=551"},"modified":"2024-12-26T22:50:37","modified_gmt":"2024-12-26T14:50:37","slug":"%e4%bd%bf%e7%94%a8pyautogui-%e6%a3%80%e6%9f%a5%e5%9b%be%e5%83%8f%e6%97%b6%ef%bc%9a%e6%98%af%e5%90%a6%e4%bf%9d%e7%95%99-except-%e5%ad%90%e5%8f%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/26\/%e4%bd%bf%e7%94%a8pyautogui-%e6%a3%80%e6%9f%a5%e5%9b%be%e5%83%8f%e6%97%b6%ef%bc%9a%e6%98%af%e5%90%a6%e4%bf%9d%e7%95%99-except-%e5%ad%90%e5%8f%a5%ef%bc%9f\/","title":{"rendered":"\u4f7f\u7528PyAutoGUI \u68c0\u67e5\u56fe\u50cf\u65f6\uff1a\u662f\u5426\u4fdd\u7559\u00a0except\u00a0\u5b50\u53e5\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528 <code>PyAutoGUI<\/code> \u68c0\u67e5\u56fe\u50cf\u65f6\uff0c\u4fdd\u7559\u6216\u79fb\u9664 <code>except<\/code> \u5b50\u53e5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u7684\u5065\u58ee\u6027\u8981\u6c42\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u5efa\u8bae\u4e0e\u80cc\u666f\u4fe1\u606f\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4f7f\u7528\u573a\u666f\uff1a\u56fe\u50cf\u5339\u914d\u4e0e\u5f02\u5e38\u5904\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 PyAutoGUI \u4e2d\uff0c\u901a\u5e38\u4f7f\u7528 <code>pyautogui.locateOnScreen()<\/code> \u6216\u7c7b\u4f3c\u65b9\u6cd5\u67e5\u627e\u56fe\u50cf\u3002\u5f53\u672a\u627e\u5230\u76ee\u6807\u56fe\u50cf\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u8fd4\u56de <code>None<\/code>\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u5904\u7406\u53ef\u80fd\u4e0d\u662f\u7edd\u5bf9\u5fc5\u8981\u7684\uff0c\u4f46\u53ef\u4ee5\u4e3a\u4ee3\u7801\u903b\u8f91\u63d0\u4f9b\u989d\u5916\u7684\u4fdd\u62a4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5173\u952e\u70b9\uff1a\u662f\u5426\u4fdd\u7559 <code>except<\/code> \u5b50\u53e5<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5e38\u89c4\u56fe\u50cf\u68c0\u6d4b\u573a\u666f\uff08\u975e\u5f02\u5e38\u89e6\u53d1\uff09<\/strong><br>\u5f53\u4ec5\u9700\u68c0\u67e5\u56fe\u50cf\u662f\u5426\u5b58\u5728\u65f6\uff0c\u4e0d\u4e00\u5b9a\u9700\u8981 <code>try...except<\/code> \u5b50\u53e5\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import pyautogui\n\n   # \u68c0\u67e5\u56fe\u50cf\u662f\u5426\u5b58\u5728\n   location = pyautogui.locateOnScreen(\"image.png\")\n   if location:\n       print(\"\u56fe\u50cf\u5df2\u627e\u5230:\", location)\n   else:\n       print(\"\u672a\u627e\u5230\u56fe\u50cf\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5355\u660e\u4e86\uff0c\u65e0\u9700\u6355\u83b7\u5f02\u5e38\u3002<\/li>\n\n\n\n<li>\u76f4\u63a5\u901a\u8fc7\u8fd4\u56de\u503c\u5904\u7406\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9700\u8981\u5904\u7406\u5f02\u5e38\u7684\u573a\u666f<\/strong><br>\u5982\u679c\u4f7f\u7528 <code>pyautogui<\/code> \u7684\u67d0\u4e9b\u51fd\u6570\u53ef\u80fd\u89e6\u53d1\u5f02\u5e38\uff08\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u95ee\u9898\uff09\uff0c\u5efa\u8bae\u4f7f\u7528 <code>try...except<\/code> \u6765\u6355\u83b7\u7279\u5b9a\u5f02\u5e38\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   import pyautogui\n\n   try:\n       location = pyautogui.locateOnScreen(\"image.png\")\n       if location:\n           print(\"\u56fe\u50cf\u5df2\u627e\u5230:\", location)\n       else:\n           print(\"\u672a\u627e\u5230\u56fe\u50cf\")\n   except pyautogui.ImageNotFoundException:  # \u5982\u679c\u6709\u81ea\u5b9a\u4e49\u5f02\u5e38\n       print(\"\u56fe\u50cf\u6587\u4ef6\u65e0\u6cd5\u627e\u5230\u6216\u8bfb\u53d6\")\n   except Exception as e:\n       print(f\"\u53d1\u751f\u672a\u77e5\u9519\u8bef: {e}\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u80fd\u591f\u6355\u83b7\u56e0\u5f02\u5e38\u5bfc\u81f4\u7684\u5d29\u6e83\u3002<\/li>\n\n\n\n<li>\u6709\u52a9\u4e8e\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u7cfb\u7edf\u7ea7\u95ee\u9898\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u590d\u6742\u81ea\u52a8\u5316\u573a\u666f\u4e2d\u7684\u5efa\u8bae<\/strong><br>\u5982\u679c\u4ee3\u7801\u4f1a\u56e0\u67d0\u4e9b\u610f\u5916\u884c\u4e3a\u5bfc\u81f4\u6267\u884c\u4e2d\u65ad\uff08\u5982\u56fe\u50cf\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\uff09\uff0c\u4f7f\u7528 <code>try...except<\/code> \u662f\u5fc5\u8981\u7684\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   try:\n       location = pyautogui.locateOnScreen(\"non_existent_image.png\")\n   except FileNotFoundError:\n       print(\"\u56fe\u50cf\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\")\n   except Exception as e:\n       print(f\"\u610f\u5916\u9519\u8bef: {e}\")\n   else:\n       if location:\n           print(\"\u56fe\u50cf\u4f4d\u7f6e:\", location)\n       else:\n           print(\"\u56fe\u50cf\u672a\u627e\u5230\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u7efc\u5408\u5efa\u8bae<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u5355\u68c0\u6d4b<\/strong>\uff1a\u5982\u679c\u53ea\u5173\u6ce8\u56fe\u50cf\u662f\u5426\u5b58\u5728\uff0c\u901a\u5e38\u4e0d\u9700\u8981 <code>except<\/code> \u5b50\u53e5\u3002<\/li>\n\n\n\n<li><strong>\u7a33\u5065\u4ee3\u7801<\/strong>\uff1a\u5f53\u5b58\u5728\u6587\u4ef6\u8def\u5f84\u3001\u6743\u9650\u6216\u5176\u4ed6\u6f5c\u5728\u95ee\u9898\u65f6\uff0c\u5e94\u6dfb\u52a0 <code>try...except<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u9700\u6c42<\/strong>\uff1a\u53ef\u7ed3\u5408\u65e5\u5fd7\u8bb0\u5f55\uff08\u5982 <code>logging<\/code> \u6a21\u5757\uff09\u6355\u83b7\u5f02\u5e38\u5e76\u8bb0\u5f55\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fdd\u7559 <code>except<\/code> \u5b50\u53e5\u7684\u5173\u952e\u662f\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5982\u679c\u53ef\u80fd\u53d1\u751f\u6f5c\u5728\u95ee\u9898\u4e14\u9700\u8981\u54cd\u5e94\uff0c<code>except<\/code> \u662f\u5fc5\u987b\u7684\uff1b\u5982\u679c\u4e0d\u5b58\u5728\u6f5c\u5728\u5f02\u5e38\uff0c\u76f4\u63a5\u901a\u8fc7\u8fd4\u56de\u503c\u5224\u65ad\u8db3\u77e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528 PyAutoGUI \u68c0\u67e5\u56fe\u50cf\u65f6\uff0c\u4fdd\u7559\u6216\u79fb\u9664 except \u5b50\u53e5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u7684\u5065\u58ee\u6027\u8981\u6c42\u3002\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}