{"id":1128633,"date":"2025-01-08T20:22:26","date_gmt":"2025-01-08T12:22:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128633.html"},"modified":"2025-01-08T20:22:28","modified_gmt":"2025-01-08T12:22:28","slug":"python%e7%88%ac%e8%99%ab%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96%e9%a1%b5%e9%9d%a2%e4%ba%8c%e7%bb%b4%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128633.html","title":{"rendered":"python\u722c\u866b\u5982\u4f55\u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095307\/99376ad0-19fd-407e-9b00-2fe769017c6f.webp\" alt=\"python\u722c\u866b\u5982\u4f55\u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801\" \/><\/p>\n<p><p> <strong>Python\u722c\u866b\u5982\u4f55\u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801\u662f\u4e00\u9879\u5177\u6709\u6311\u6218\u6027\u7684\u4efb\u52a1\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528Python\u722c\u866b\u548c\u4e00\u4e9b\u76f8\u5173\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u8bc6\u522b\u548c\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247\u3001\u4fdd\u5b58\u6216\u5904\u7406\u4e8c\u7ef4\u7801\u4fe1\u606f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong>\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u6211\u4eec\u662f\u5426\u80fd\u591f\u51c6\u786e\u5730\u627e\u5230\u5e76\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d1\u9001HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7f51\u9875\u5185\u5bb9\u6293\u53d6\u65f6\uff0c\u53d1\u9001HTTP\u8bf7\u6c42\u662f\u7b2c\u4e00\u6b65\u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<code>requests<\/code>\u5e93\u4f7f\u5f97\u53d1\u9001HTTP\u8bf7\u6c42\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Successfully fetched the webpage!&quot;)<\/p>\n<p>    webpage_content = response.text<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to fetch the webpage. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u68c0\u67e5\u4e86\u54cd\u5e94\u7684\u72b6\u6001\u7801\u3002\u5982\u679c\u72b6\u6001\u7801\u662f200\uff08OK\uff09\uff0c\u5219\u610f\u5473\u7740\u8bf7\u6c42\u6210\u529f\uff0c\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u5904\u7406\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u8981\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790HTML\u5185\u5bb9\u3002\u8fd9\u901a\u5e38\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<code>BeautifulSoup<\/code>\u80fd\u591f\u8f7b\u677e\u5730\u904d\u5386\u548c\u641c\u7d22HTML\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(webpage_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u5047\u8bbe\u4e8c\u7ef4\u7801\u56fe\u7247\u5728\u4e00\u4e2a&lt;img&gt;\u6807\u7b7e\u5185\uff0c\u5e76\u4e14\u6709\u4e00\u4e2a\u72ec\u7279\u7684\u7c7b\u540d\u6216id<\/strong><\/h2>\n<p>qr_code_img = soup.find(&#39;img&#39;, class_=&#39;qrcode-class&#39;)<\/p>\n<p>if qr_code_img:<\/p>\n<p>    qr_code_url = qr_code_img[&#39;src&#39;]<\/p>\n<p>    print(f&quot;Found QR code image URL: {qr_code_url}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;QR code image not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u4e8c\u7ef4\u7801\u56fe\u7247\u4f4d\u4e8e\u4e00\u4e2a\u5177\u6709\u72ec\u7279\u7c7b\u540d\u7684<code>&lt;img&gt;<\/code>\u6807\u7b7e\u5185\u3002\u6211\u4eec\u4f7f\u7528<code>BeautifulSoup<\/code>\u7684<code>find<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u8fd9\u4e2a\u6807\u7b7e\uff0c\u5e76\u63d0\u53d6\u5176<code>src<\/code>\u5c5e\u6027\uff0c\u8fd9\u901a\u5e38\u662f\u56fe\u7247\u7684URL\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bc6\u522b\u548c\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u4e8c\u7ef4\u7801\u56fe\u7247\u7684URL\uff0c\u5c31\u53ef\u4ee5\u4e0b\u8f7d\u5e76\u8bc6\u522b\u5b83\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u4e0b\u8f7d\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>Pillow<\/code>\u5e93\u6253\u5f00\u548c\u4fdd\u5b58\u56fe\u7247\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>from io import BytesIO<\/p>\n<p>qr_code_response = requests.get(qr_code_url)<\/p>\n<p>if qr_code_response.status_code == 200:<\/p>\n<p>    qr_code_image = Image.open(BytesIO(qr_code_response.content))<\/p>\n<p>    qr_code_image.save(&#39;qrcode.png&#39;)<\/p>\n<p>    print(&quot;QR code image saved successfully!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;Failed to download the QR code image. Status code: {qr_code_response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\u6765\u4e0b\u8f7d\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>Pillow<\/code>\u5e93\u7684<code>Image<\/code>\u6a21\u5757\u6253\u5f00\u548c\u4fdd\u5b58\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u4e8c\u7ef4\u7801\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5e76\u4fdd\u5b58\u4e8c\u7ef4\u7801\u56fe\u7247\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>zxing<\/code>\u6216<code>pyzbar<\/code>\u5e93\u6765\u89e3\u7801\u4e8c\u7ef4\u7801\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>pyzbar<\/code>\u5e93\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyzbar.pyzbar import decode<\/p>\n<p>decoded_info = decode(qr_code_image)<\/p>\n<p>if decoded_info:<\/p>\n<p>    print(f&quot;Decoded QR code information: {decoded_info[0].data.decode(&#39;utf-8&#39;)}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to decode the QR code.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pyzbar<\/code>\u7684<code>decode<\/code>\u65b9\u6cd5\u6765\u89e3\u7801\u4e8c\u7ef4\u7801\u4fe1\u606f\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u7f51\u9875\u4e2d\u7684\u4e8c\u7ef4\u7801\u56fe\u7247\uff0c\u5e76\u89e3\u7801\u5176\u4e2d\u7684\u4fe1\u606f\u3002\u8fd9\u4e00\u8fc7\u7a0b\u6d89\u53ca\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u8bc6\u522b\u548c\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247\u3001\u4ee5\u53ca\u5904\u7406\u4e8c\u7ef4\u7801\u4fe1\u606f\u3002<strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u6211\u4eec\u662f\u5426\u80fd\u591f\u51c6\u786e\u627e\u5230\u5e76\u63d0\u53d6\u4e8c\u7ef4\u7801\u56fe\u7247\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528<code>requests<\/code>\u3001<code>BeautifulSoup<\/code>\u3001<code>Pillow<\/code>\u548c<code>pyzbar<\/code>\u7b49\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u987a\u5229\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u866b\u8bc6\u522b\u548c\u4e0b\u8f7d\u7f51\u9875\u4e2d\u7684\u4e8c\u7ef4\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u56fe\u50cf\u5904\u7406\u5e93\uff0c\u5982Pillow\u548cOpenCV\uff0c\u7ed3\u5408\u4e8c\u7ef4\u7801\u8bc6\u522b\u5e93\uff0c\u4f8b\u5982pyzbar\u6216opencv-python\uff0c\u6765\u8bc6\u522b\u548c\u4e0b\u8f7d\u7f51\u9875\u4e2d\u7684\u4e8c\u7ef4\u7801\u3002\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u6293\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u627e\u5230\u4e8c\u7ef4\u7801\u7684URL\u6216\u4e0b\u8f7d\u4e8c\u7ef4\u7801\u56fe\u50cf\uff0c\u63a5\u7740\u5229\u7528\u4e8c\u7ef4\u7801\u8bc6\u522b\u5e93\u63d0\u53d6\u4e8c\u7ef4\u7801\u4e2d\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6\u4e8c\u7ef4\u7801\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u6280\u672f\u6311\u6218\uff1f<\/strong><br \/>\u6293\u53d6\u4e8c\u7ef4\u7801\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u4e2a\u6280\u672f\u6311\u6218\uff0c\u5305\u62ec\u7f51\u9875\u7684\u52a8\u6001\u52a0\u8f7d\u3001\u9a8c\u8bc1\u7801\u4fdd\u62a4\u4ee5\u53ca\u56fe\u7247\u683c\u5f0f\u95ee\u9898\u7b49\u3002\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u8fdb\u884c\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u6b63\u786e\u83b7\u53d6\u4e8c\u7ef4\u7801\u56fe\u50cf\u3002\u9a8c\u8bc1\u7801\u4fdd\u62a4\u5219\u9700\u8981\u989d\u5916\u7684\u5904\u7406\uff0c\u901a\u5e38\u9700\u8981\u624b\u52a8\u8f93\u5165\u6216\u4f7f\u7528OCR\u6280\u672f\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6293\u53d6\u7684\u4e8c\u7ef4\u7801\u4fe1\u606f\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6293\u53d6\u7684\u4e8c\u7ef4\u7801\u4fe1\u606f\u51c6\u786e\u5b8c\u6574\uff0c\u53ef\u4ee5\u91c7\u53d6\u591a\u79cd\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u9009\u62e9\u9ad8\u8d28\u91cf\u7684\u4e8c\u7ef4\u7801\u56fe\u50cf\uff0c\u907f\u514d\u6a21\u7cca\u6216\u5931\u771f\u7684\u56fe\u50cf\u5f71\u54cd\u8bc6\u522b\u3002\u5176\u6b21\uff0c\u4f7f\u7528\u591a\u4e2a\u4e8c\u7ef4\u7801\u8bc6\u522b\u5e93\u8fdb\u884c\u6bd4\u5bf9\uff0c\u4ee5\u63d0\u9ad8\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u53bb\u91cd\uff0c\u4ee5\u786e\u4fdd\u63d0\u53d6\u7684\u4fe1\u606f\u662f\u552f\u4e00\u4e14\u6709\u6548\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u5982\u4f55\u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801 \u6293\u53d6\u9875\u9762\u4e8c\u7ef4\u7801\u662f\u4e00\u9879\u5177\u6709\u6311\u6218\u6027\u7684\u4efb\u52a1\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528Python\u722c\u866b\u548c\u4e00\u4e9b\u76f8\u5173 [&hellip;]","protected":false},"author":3,"featured_media":1128639,"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\/1128633"}],"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=1128633"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128633\/revisions"}],"predecessor-version":[{"id":1128640,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128633\/revisions\/1128640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128639"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}