{"id":953780,"date":"2024-12-27T01:47:53","date_gmt":"2024-12-26T17:47:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953780.html"},"modified":"2024-12-27T01:47:55","modified_gmt":"2024-12-26T17:47:55","slug":"python-itchat-%e5%a6%82%e4%bd%95%e5%8f%91%e9%80%81%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953780.html","title":{"rendered":"python itchat \u5982\u4f55\u53d1\u9001\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094140\/bdc5cd0d-c718-4dc1-ac09-a4e5e7101b35.webp\" alt=\"python itchat \u5982\u4f55\u53d1\u9001\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528Itchat\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u7684\u65b9\u6cd5\u662f\uff0c\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u4e2d\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801\u3001\u5728\u53d1\u9001\u6d88\u606f\u65f6\u4f7f\u7528\u6b63\u786e\u7684API\u3001\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u7f16\u7801\u95ee\u9898\u3002<\/strong><br \/>\u5176\u4e2d\uff0c\u786e\u4fdd\u5b57\u7b26\u7f16\u7801\u6b63\u786e\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3aPython\u9ed8\u8ba4\u7684\u7f16\u7801\u8bbe\u7f6e\u53ef\u80fd\u5bfc\u81f4\u5728\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u65f6\u51fa\u73b0\u95ee\u9898\u3002\u4e3a\u4e86\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u80fd\u591f\u6b63\u786e\u5730\u5904\u7406\u548c\u53d1\u9001\u4e2d\u6587\u6d88\u606f\uff0c\u60a8\u53ef\u4ee5\u5728\u811a\u672c\u7684\u5f00\u5934\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u884c\u6765\u6307\u5b9aUTF-8\u7f16\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5982\u4f55\u4f7f\u7528Itchat\u53d1\u9001\u4e2d\u6587\u6d88\u606f<\/strong><\/p>\n<\/p>\n<p><p>Itchat\u662f\u4e00\u4e2a\u7528\u4e8e\u5fae\u4fe1\u4e2a\u4eba\u53f7\u63a5\u53e3\u7684\u5f00\u6e90\u5e93\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u5b9e\u73b0\u5fae\u4fe1\u6d88\u606f\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Itchat\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Itchat<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Itchat\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u6b64\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install itchat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u767b\u5f55\u5fae\u4fe1<\/h3>\n<\/p>\n<p><p>\u5728\u53d1\u9001\u6d88\u606f\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u901a\u8fc7Itchat\u767b\u5f55\u60a8\u7684\u5fae\u4fe1\u8d26\u53f7\u3002Itchat\u4f1a\u751f\u6210\u4e00\u4e2a\u4e8c\u7ef4\u7801\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5fae\u4fe1\u626b\u63cf\u8be5\u4e8c\u7ef4\u7801\u8fdb\u884c\u767b\u5f55\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u767b\u5f55\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itchat<\/p>\n<p>itchat.auto_login(hotReload=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>hotReload=True<\/code>\u53c2\u6570\u7528\u4e8e\u5728\u7a0b\u5e8f\u9000\u51fa\u540e\u4e0d\u9700\u8981\u91cd\u65b0\u626b\u63cf\u4e8c\u7ef4\u7801\u767b\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u53d1\u9001\u4e2d\u6587\u6d88\u606f<\/h3>\n<\/p>\n<p><p>\u767b\u5f55\u6210\u529f\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Itchat\u7684<code>send<\/code>\u65b9\u6cd5\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u53d1\u9001\u6d88\u606f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itchat<\/p>\n<p>itchat.auto_login(hotReload=True)<\/p>\n<h2><strong>\u53d1\u9001\u6587\u672c\u6d88\u606f<\/strong><\/h2>\n<p>itchat.send(&#39;\u4f60\u597d\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u6d88\u606f&#39;, toUserName=&#39;filehelper&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>itchat.send<\/code>\u65b9\u6cd5\u53d1\u9001\u4e86\u4e00\u6761\u4e2d\u6587\u6d88\u606f\u7ed9\u6587\u4ef6\u52a9\u624b\uff08<code>filehelper<\/code>\uff09\uff0c\u60a8\u4e5f\u53ef\u4ee5\u5c06<code>toUserName<\/code>\u53c2\u6570\u66ff\u6362\u4e3a\u5176\u4ed6\u5fae\u4fe1\u7528\u6237\u7684\u7528\u6237\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u83b7\u53d6\u8054\u7cfb\u4eba\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u83b7\u53d6\u60a8\u7684\u5fae\u4fe1\u597d\u53cb\u6216\u7fa4\u804a\u5217\u8868\uff0c\u7136\u540e\u5411\u7279\u5b9a\u7684\u8054\u7cfb\u4eba\u53d1\u9001\u6d88\u606f\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u60a8\u7684\u8054\u7cfb\u4eba\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">friends = itchat.get_friends(update=True)<\/p>\n<p>for friend in friends:<\/p>\n<p>    print(friend[&#39;NickName&#39;], friend[&#39;UserName&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u6839\u636e\u83b7\u53d6\u5230\u7684<code>NickName<\/code>\u6216<code>UserName<\/code>\u6765\u786e\u5b9a\u60a8\u8981\u53d1\u9001\u6d88\u606f\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u53d1\u9001\u6d88\u606f\u7ed9\u7279\u5b9a\u8054\u7cfb\u4eba<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u60a8\u60f3\u8981\u5411\u7279\u5b9a\u6635\u79f0\u7684\u597d\u53cb\u53d1\u9001\u6d88\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">friend_name = &#39;\u597d\u53cb\u6635\u79f0&#39;<\/p>\n<p>friend = itchat.search_friends(name=friend_name)<\/p>\n<p>if friend:<\/p>\n<p>    friend_user_name = friend[0][&#39;UserName&#39;]<\/p>\n<p>    itchat.send(&#39;\u4f60\u597d\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u6d88\u606f&#39;, toUserName=friend_user_name)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;\u6ca1\u6709\u627e\u5230\u8be5\u597d\u53cb&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u53d1\u9001\u5931\u8d25\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Itchat\u53d1\u9001\u6d88\u606f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\u6216\u5176\u4ed6\u539f\u56e0\u5bfc\u81f4\u7684\u53d1\u9001\u5931\u8d25\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u9700\u8981\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4f8b\u5982\u91cd\u8bd5\u673a\u5236\u6216\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u9000\u51fa\u767b\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u53d1\u9001\u5b8c\u6d88\u606f\u540e\uff0c\u5982\u679c\u4e0d\u518d\u9700\u8981\u4fdd\u6301\u767b\u5f55\u72b6\u6001\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u9000\u51fa\u767b\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">itchat.logout()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684Itchat\u5e93\u8f7b\u677e\u5730\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u60a8\u7684\u811a\u672c\u4e2d\u8bbe\u7f6e\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u6d88\u606f\u53d1\u9001\u903b\u8f91\u548c\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4ee5\u786e\u4fdd\u6d88\u606f\u80fd\u591f\u987a\u5229\u53d1\u9001\u5230\u76ee\u6807\u8054\u7cfb\u4eba\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Itchat\u65f6\uff0c\u8bf7\u6ce8\u610f\u5fae\u4fe1\u7684\u76f8\u5173\u4f7f\u7528\u653f\u7b56\u548c\u9650\u5236\uff0c\u907f\u514d\u56e0\u9891\u7e41\u53d1\u9001\u6d88\u606f\u800c\u5bfc\u81f4\u8d26\u53f7\u88ab\u9650\u5236\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528 Python itchat \u53d1\u9001\u4e2d\u6587\u6d88\u606f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528 Python itchat \u53d1\u9001\u4e2d\u6587\u6d88\u606f\u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u5b57\u7b26\u4e32\u91c7\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f7f\u7528 Unicode \u7f16\u7801\u53ef\u4ee5\u907f\u514d\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u4e3a\u4e71\u7801\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e2d\u6587\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9 itchat \u7684\u53d1\u9001\u65b9\u6cd5\uff1a<code>itchat.send(&#39;\u4f60\u597d&#39;, toUserName=&#39;\u76ee\u6807\u7528\u6237\u7684\u7528\u6237\u540d&#39;)<\/code>\u3002<\/p>\n<p><strong>itchat \u53d1\u9001\u4e2d\u6587\u6d88\u606f\u65f6\u9047\u5230\u4e71\u7801\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u65f6\u9047\u5230\u4e71\u7801\u95ee\u9898\uff0c\u68c0\u67e5\u4f60\u7684 Python \u73af\u5883\u662f\u5426\u652f\u6301 UTF-8 \u7f16\u7801\u3002\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u6587\u4ef6\u4ee5 UTF-8 \u683c\u5f0f\u4fdd\u5b58\uff0c\u4e14\u5728\u53d1\u9001\u6d88\u606f\u65f6\u4f7f\u7528 Unicode \u5b57\u7b26\u4e32\u3002\u5982\u679c\u95ee\u9898\u4f9d\u7136\u5b58\u5728\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728\u53d1\u9001\u524d\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u7801\u5904\u7406\uff0c\u5982\u4f7f\u7528 <code>str.encode(&#39;utf-8&#39;)<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528 itchat \u53d1\u9001\u4e2d\u6587\u6d88\u606f\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u65f6\uff0c\u5e94\u786e\u4fdd\u76ee\u6807\u7528\u6237\u7684\u7528\u6237\u540d\u6b63\u786e\u65e0\u8bef\uff0c\u4ee5\u907f\u514d\u6d88\u606f\u53d1\u9001\u5931\u8d25\u3002\u8fd8\u9700\u786e\u4fdd\u4f60\u7684\u7f51\u7edc\u8fde\u63a5\u7a33\u5b9a\uff0c\u907f\u514d\u56e0\u7f51\u7edc\u95ee\u9898\u5bfc\u81f4\u6d88\u606f\u53d1\u9001\u5ef6\u8fdf\u6216\u5931\u8d25\u3002\u6b64\u5916\uff0c\u4f7f\u7528 itchat \u53d1\u9001\u6d88\u606f\u65f6\uff0c\u9700\u9075\u5b88\u5fae\u4fe1\u7684\u4f7f\u7528\u653f\u7b56\uff0c\u907f\u514d\u56e0\u9891\u7e41\u53d1\u9001\u76f8\u540c\u5185\u5bb9\u800c\u88ab\u9650\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528Itchat\u53d1\u9001\u4e2d\u6587\u6d88\u606f\u7684\u65b9\u6cd5\u662f\uff0c\u786e\u4fdd\u60a8\u7684Python\u811a\u672c\u4e2d\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801\u3001\u5728\u53d1\u9001\u6d88\u606f [&hellip;]","protected":false},"author":3,"featured_media":953784,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953780"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=953780"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953780\/revisions"}],"predecessor-version":[{"id":953789,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953780\/revisions\/953789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953784"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}