{"id":1168055,"date":"2025-01-15T15:53:14","date_gmt":"2025-01-15T07:53:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168055.html"},"modified":"2025-01-15T15:53:17","modified_gmt":"2025-01-15T07:53:17","slug":"python%e5%a6%82%e4%bd%95%e7%99%bb%e5%bd%95%e7%94%b7%e5%8f%8b%e5%be%ae%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168055.html","title":{"rendered":"python\u5982\u4f55\u767b\u5f55\u7537\u53cb\u5fae\u4fe1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065417\/c431dafe-9795-47d9-92a9-16cda9ad119f.webp\" alt=\"python\u5982\u4f55\u767b\u5f55\u7537\u53cb\u5fae\u4fe1\" \/><\/p>\n<p><p> <strong>Python\u767b\u5f55\u7537\u53cb\u5fae\u4fe1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982ItChat\u3001\u5229\u7528\u5fae\u4fe1\u673a\u5668\u4eba\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u662f\u4e00\u79cd\u8f83\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5176\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u767b\u5f55\u5fae\u4fe1\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u9996\u5148\u83b7\u53d6\u4e8c\u7ef4\u7801\uff0c\u7136\u540e\u8fdb\u884c\u626b\u7801\u767b\u5f55\uff0c\u6700\u540e\u4fdd\u6301\u4f1a\u8bdd\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u4e8c\u7ef4\u7801<\/strong>\uff1a\u4f7f\u7528requests\u5e93\u5411\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u767b\u5f55\u4e8c\u7ef4\u7801\uff0c\u5e76\u5c06\u5176\u663e\u793a\u7ed9\u7528\u6237\u3002\u7528\u6237\u4f7f\u7528\u5fae\u4fe1\u626b\u63cf\u8be5\u4e8c\u7ef4\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u626b\u7801\u767b\u5f55<\/strong>\uff1a\u5728\u7528\u6237\u626b\u63cf\u4e8c\u7ef4\u7801\u540e\uff0c\u5fae\u4fe1\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u4e00\u4e2aUUID\uff0c\u5229\u7528\u8fd9\u4e2aUUID\u8fdb\u884c\u767b\u5f55\u8bf7\u6c42\uff0c\u83b7\u53d6\u5230\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u4f1a\u8bdd<\/strong>\uff1a\u767b\u5f55\u6210\u529f\u540e\uff0c\u5fae\u4fe1\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u4e00\u7ec4cookies\uff0c\u8fd9\u4e9bcookies\u9700\u8981\u4fdd\u6301\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u7684\u8bf7\u6c42\u4e2d\u7ef4\u6301\u4f1a\u8bdd\u72b6\u6001\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3<\/h3>\n<\/p>\n<p><h4>1. \u51c6\u5907\u5de5\u4f5c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684Python\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install requests<\/p>\n<p>pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u767b\u5f55\u4e8c\u7ef4\u7801<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7requests\u5e93\u5411\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u767b\u5f55\u4e8c\u7ef4\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>import time<\/p>\n<p>from PIL import Image<\/p>\n<p>from io import BytesIO<\/p>\n<h2><strong>\u83b7\u53d6UUID<\/strong><\/h2>\n<p>def get_uuid():<\/p>\n<p>    url = &#39;https:\/\/login.wx.qq.com\/jslogin&#39;<\/p>\n<p>    params = {<\/p>\n<p>        &#39;appid&#39;: &#39;wx782c26e4c19acffb&#39;,<\/p>\n<p>        &#39;fun&#39;: &#39;new&#39;,<\/p>\n<p>        &#39;lang&#39;: &#39;zh_CN&#39;,<\/p>\n<p>        &#39;_&#39;: int(time.time())<\/p>\n<p>    }<\/p>\n<p>    response = requests.get(url, params=params)<\/p>\n<p>    code = response.text.split(&#39;=&#39;)[1].strip().strip(&#39;&quot;;&#39;)<\/p>\n<p>    return code<\/p>\n<h2><strong>\u83b7\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247<\/strong><\/h2>\n<p>def get_qr_code(uuid):<\/p>\n<p>    url = f&#39;https:\/\/login.weixin.qq.com\/qrcode\/{uuid}&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    img = Image.open(BytesIO(response.content))<\/p>\n<p>    img.show()<\/p>\n<p>uuid = get_uuid()<\/p>\n<p>get_qr_code(uuid)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u626b\u7801\u767b\u5f55<\/h4>\n<\/p>\n<p><p>\u7528\u6237\u4f7f\u7528\u5fae\u4fe1\u626b\u63cf\u4e8c\u7ef4\u7801\u540e\uff0c\u5fae\u4fe1\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u4e00\u4e2aUUID\uff0c\u5229\u7528\u8fd9\u4e2aUUID\u8fdb\u884c\u767b\u5f55\u8bf7\u6c42\uff0c\u83b7\u53d6\u5230\u7528\u6237\u7684\u767b\u5f55\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6e\u8be2\u626b\u7801\u7ed3\u679c<\/p>\n<p>def w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t_for_login(uuid):<\/p>\n<p>    url = f&#39;https:\/\/login.wx.qq.com\/cgi-bin\/mmwebwx-bin\/login?tip=1&amp;uuid={uuid}&amp;_={int(time.time())}&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    code = response.text.split(&#39;=&#39;)[1].split(&#39;;&#39;)[0].strip()<\/p>\n<p>    return code<\/p>\n<h2><strong>\u7b49\u5f85\u7528\u6237\u626b\u7801<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    code = wait_for_login(uuid)<\/p>\n<p>    if code == &#39;200&#39;:<\/p>\n<p>        print(&#39;\u767b\u5f55\u6210\u529f&#39;)<\/p>\n<p>        break<\/p>\n<p>    elif code == &#39;201&#39;:<\/p>\n<p>        print(&#39;\u8bf7\u786e\u8ba4\u767b\u5f55&#39;)<\/p>\n<p>    elif code == &#39;408&#39;:<\/p>\n<p>        print(&#39;\u4e8c\u7ef4\u7801\u8d85\u65f6\uff0c\u8bf7\u91cd\u65b0\u83b7\u53d6&#39;)<\/p>\n<p>        uuid = get_uuid()<\/p>\n<p>        get_qr_code(uuid)<\/p>\n<p>    time.sleep(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u83b7\u53d6\u767b\u5f55\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u786e\u8ba4\u767b\u5f55\u540e\uff0c\u5fae\u4fe1\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u4e00\u7ec4cookies\uff0c\u8fd9\u4e9bcookies\u9700\u8981\u4fdd\u6301\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u7684\u8bf7\u6c42\u4e2d\u7ef4\u6301\u4f1a\u8bdd\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u767b\u5f55\u4fe1\u606f<\/p>\n<p>def get_login_info(uuid):<\/p>\n<p>    url = f&#39;https:\/\/login.wx.qq.com\/cgi-bin\/mmwebwx-bin\/login?tip=0&amp;uuid={uuid}&amp;_={int(time.time())}&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    redirect_uri = response.text.split(&#39;&quot;&#39;)[1]<\/p>\n<p>    base_uri = redirect_uri[:redirect_uri.rfind(&#39;\/&#39;)]<\/p>\n<p>    response = requests.get(redirect_uri, allow_redirects=False)<\/p>\n<p>    cookies = response.cookies<\/p>\n<p>    return base_uri, cookies<\/p>\n<p>base_uri, cookies = get_login_info(uuid)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93ItChat<\/h3>\n<\/p>\n<p><p>ItChat\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5fae\u4fe1\u4e2a\u4eba\u53f7\u63a5\u53e3\uff0c\u4f7f\u7528\u975e\u5e38\u65b9\u4fbf\u3002\u4e0b\u9762\u662f\u4f7f\u7528ItChat\u767b\u5f55\u5fae\u4fe1\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5ItChat<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">pip install itchat<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u767b\u5f55\u5fae\u4fe1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528ItChat\u767b\u5f55\u5fae\u4fe1\uff0c\u5e76\u83b7\u53d6\u7528\u6237\u7684\u57fa\u672c\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itchat<\/p>\n<h2><strong>\u767b\u5f55\u5fae\u4fe1<\/strong><\/h2>\n<p>itchat.auto_login(hotReload=True)<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u57fa\u672c\u4fe1\u606f<\/strong><\/h2>\n<p>user_info = itchat.search_friends()<\/p>\n<p>print(user_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u5fae\u4fe1\u673a\u5668\u4eba<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5f00\u6e90\u7684\u5fae\u4fe1\u673a\u5668\u4eba\u6765\u5b9e\u73b0\u767b\u5f55\u5fae\u4fe1\u548c\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u6bd4\u5982WeRoBot\u3001wxpy\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5wxpy<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">pip install wxpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u767b\u5f55\u5fae\u4fe1\u5e76\u53d1\u9001\u6d88\u606f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528wxpy\u767b\u5f55\u5fae\u4fe1\uff0c\u5e76\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from wxpy import Bot<\/p>\n<h2><strong>\u767b\u5f55\u5fae\u4fe1<\/strong><\/h2>\n<p>bot = Bot()<\/p>\n<h2><strong>\u83b7\u53d6\u597d\u53cb<\/strong><\/h2>\n<p>friend = bot.friends().search(&#39;\u597d\u53cb\u6635\u79f0&#39;)[0]<\/p>\n<h2><strong>\u53d1\u9001\u6d88\u606f<\/strong><\/h2>\n<p>friend.send(&#39;Hello, WeChat!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982ItChat\u3001\u5229\u7528\u5fae\u4fe1\u673a\u5668\u4eba<\/strong>\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0Python\u767b\u5f55\u5fae\u4fe1\u7684\u529f\u80fd\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u9700\u8981\u8f83\u591a\u7684\u624b\u52a8\u64cd\u4f5c\u548c\u7ef4\u62a4\uff0c\u4f7f\u7528ItChat\u548cwxpy\u5219\u66f4\u52a0\u65b9\u4fbf\u548c\u5feb\u6377\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u5fae\u4fe1\u64cd\u4f5c\u65f6\uff0c\u8bf7\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u5fae\u4fe1\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u4e0d\u8981\u8fdb\u884c\u975e\u6cd5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u81ea\u52a8\u767b\u5f55\u5fae\u4fe1\uff1f<\/strong><br \/>\u867d\u7136Python\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u4f46\u76f4\u63a5\u767b\u5f55\u5fae\u4fe1\u6d89\u53ca\u5230\u5b89\u5168\u6027\u548c\u9690\u79c1\u95ee\u9898\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4e00\u4e9b\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u6bd4\u5982Selenium\u7b49\uff0c\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\u3002\u4e0d\u8fc7\uff0c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u9700\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u5e76\u786e\u4fdd\u83b7\u5f97\u4ed6\u4eba\u540c\u610f\u3002<\/p>\n<p><strong>\u767b\u5f55\u5fae\u4fe1\u9700\u8981\u54ea\u4e9b\u4fe1\u606f\uff1f<\/strong><br \/>\u767b\u5f55\u5fae\u4fe1\u901a\u5e38\u9700\u8981\u7528\u6237\u7684\u624b\u673a\u53f7\u7801\u548c\u5bc6\u7801\u3002\u5982\u679c\u8bbe\u7f6e\u4e86\u53cc\u56e0\u7d20\u8ba4\u8bc1\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u9a8c\u8bc1\u7801\u3002\u4e86\u89e3\u8fd9\u4e9b\u4fe1\u606f\u6709\u52a9\u4e8e\u786e\u4fdd\u5728\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\u65f6\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u767b\u5f55\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5fae\u4fe1\u7684\u9a8c\u8bc1\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u81ea\u52a8\u767b\u5f55\u5fae\u4fe1\u65f6\uff0c\u9a8c\u8bc1\u7801\u5f80\u5f80\u662f\u4e00\u4e2a\u96be\u70b9\u3002\u53ef\u4ee5\u901a\u8fc7\u56fe\u50cf\u8bc6\u522b\u5e93\uff08\u5982Tesseract\uff09\u6765\u5904\u7406\u9a8c\u8bc1\u7801\uff0c\u6216\u8005\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801\u6765\u5b8c\u6210\u767b\u5f55\u3002\u786e\u4fdd\u5728\u8bbe\u8ba1\u811a\u672c\u65f6\u8003\u8651\u5230\u8fd9\u4e00\u70b9\uff0c\u4ee5\u63d0\u9ad8\u767b\u5f55\u6210\u529f\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u767b\u5f55\u5fae\u4fe1\u662f\u5426\u5b89\u5168\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884c\u767b\u5f55\u64cd\u4f5c\u65f6\uff0c\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u91cf\u3002\u786e\u4fdd\u5728\u7f16\u5199\u811a\u672c\u65f6\uff0c\u4e0d\u8981\u6cc4\u9732\u4e2a\u4eba\u4fe1\u606f\u6216\u5bc6\u7801\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u65f6\uff0c\u9700\u9075\u5faa\u5fae\u4fe1\u7684\u4f7f\u7528\u653f\u7b56\uff0c\u907f\u514d\u8d26\u53f7\u88ab\u5c01\u7981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u767b\u5f55\u7537\u53cb\u5fae\u4fe1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5fae\u4fe1\u7f51\u9875\u7248\u63a5\u53e3\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982ItChat\u3001\u5229\u7528\u5fae\u4fe1\u673a\u5668\u4eba\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1168061,"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\/1168055"}],"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=1168055"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168055\/revisions"}],"predecessor-version":[{"id":1168065,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168055\/revisions\/1168065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168061"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}