{"id":173604,"date":"2024-05-08T18:14:55","date_gmt":"2024-05-08T10:14:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173604.html"},"modified":"2024-05-08T18:15:07","modified_gmt":"2024-05-08T10:15:07","slug":"python%e7%88%ac%e5%8f%96%e6%97%b6%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9egbk","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173604.html","title":{"rendered":"python\u722c\u53d6\u65f6\u5982\u4f55\u8fd4\u56degbk"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045617\/88c88993-3540-4c12-b94f-e24f2cbba69f.webp\" alt=\"python\u722c\u53d6\u65f6\u5982\u4f55\u8fd4\u56degbk\" \/><\/p>\n<p><p>GBK \u7f16\u7801\u662f\u9488\u5bf9\u7b80\u4f53\u4e2d\u6587\u7684\u6269\u5c55\u5b57\u7b26\u96c6\uff0c\u800c\u5728\u4f7f\u7528 Python \u8fdb\u884c\u7f51\u7edc\u722c\u866b\u65f6\u5904\u7406GBK\u7f16\u7801\u901a\u5e38\u662f\u4e3a\u4e86\u6b63\u786e\u89e3\u6790\u4e2d\u6587\u7f51\u9875\u5185\u5bb9\u3002\u8981\u4f7f Python \u722c\u866b\u8fd4\u56de GBK \u7f16\u7801\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u501f\u52a9\u7f16\u7801\u8f6c\u6362\u7684\u65b9\u6cd5\uff0c<strong>\u9996\u5148\u4f7f\u7528\u722c\u866b\u5de5\u5177\u83b7\u53d6\u5185\u5bb9\u3001\u968f\u540e\u68c0\u6d4b\u7f16\u7801\u3001\u6700\u540e\u8fdb\u884c\u7f16\u7801\u8f6c\u6362<\/strong>\u3002<strong>\u4f7f\u7528 requests \u5e93\u83b7\u53d6\u5185\u5bb9\u65f6<\/strong>\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u54cd\u5e94\u5934\u6216\u5185\u5bb9\u6765\u786e\u5b9a\u7f16\u7801\uff0c\u518d\u4f7f\u7528\u76f8\u5e94\u7684\u7f16\u7801\u89e3\u6790\u5185\u5bb9\u3002\u5982\u679c\u9700\u8981\u5c06\u5185\u5bb9\u8f6c\u6362\u4e3a GBK\uff0c\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u7f16\u7801\u529f\u80fd\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u8981\u722c\u53d6\u7f51\u9875\uff0c\u9996\u5148\u8981\u4f7f\u7528\u4e00\u4e2a\u5e93\u6765\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\u3002\u5728 Python \u4e2d\uff0crequests \u5e93\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u5e76\u4e14\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;http:\/\/example.com&quot;  # \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528GBK\u7f16\u7801\u7684\u4e2d\u6587\u7f51\u9875<\/p>\n<p>response = requests.get(url)<\/p>\n<p>response.encoding = &#039;GBK&#039;  # \u660e\u786e\u8bbe\u7f6e\u54cd\u5e94\u7f16\u7801\u4e3aGBK<\/p>\n<p>content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u53d1\u9001\u4e00\u4e2a GET \u8bf7\u6c42\u5230\u6307\u5b9a\u7684 URL\uff0c\u7136\u540e\u8bbe\u7f6e\u54cd\u5e94\u7684\u7f16\u7801\u4e3a GBK\uff0c\u6700\u540e\u83b7\u53d6\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u68c0\u6d4b\u548c\u8f6c\u6362\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u7f16\u7801\uff0c\u4f46\u6709\u65f6\u5019\u54cd\u5e94\u5934\u4e2d\u5e76\u6ca1\u6709\u6b63\u786e\u7684\u7f16\u7801\u4fe1\u606f\u3002\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e93\u4f8b\u5982 <code>chardet<\/code> \u6765\u68c0\u6d4b\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import chardet<\/p>\n<h2><strong>...\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9<\/strong><\/h2>\n<h2><strong>\u68c0\u6d4b\u7f16\u7801<\/strong><\/h2>\n<p>detected_encoding = chardet.detect(response.content)[&#039;encoding&#039;]<\/p>\n<p>if detected_encoding != &#039;utf-8&#039;:<\/p>\n<p>    content = response.content.decode(&#039;GBK&#039;, errors=&#039;ignore&#039;)  # \u5ffd\u7565\u975e\u6cd5\u5b57\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u68c0\u6d4b\u5230\u7684\u7f16\u7801\u4e0d\u662f UTF-8\uff0c\u5e76\u4e14\u4f60\u5e0c\u671b\u5b83\u662f GBK\uff0c\u90a3\u4e48\u53ef\u4ee5\u6309\u7167\u8fd9\u79cd\u65b9\u5f0f\u8fdb\u884c\u89e3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u51b3\u7f16\u7801\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u7f51\u9875\u65f6\uff0c\u5982\u679c\u4e0d\u6b63\u786e\u8bbe\u5b9a\u6216\u8bc6\u522b\u7f16\u7801\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u4e71\u7801\u3002\u89e3\u51b3\u6b64\u7c7b\u7f16\u7801\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c1d\u8bd5\u591a\u79cd\u7f16\u7801\u8fdb\u884c\u89e3\u7801\uff0c\u76f4\u5230\u627e\u5230\u6b63\u786e\u7684\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u5b9a\u5df2\u7ecf\u83b7\u53d6 response.content<\/p>\n<h2><strong>\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u8fdb\u884c\u89e3\u7801\uff0c\u76f4\u5230\u6210\u529f<\/strong><\/h2>\n<p>for encoding in (&#039;utf-8&#039;, &#039;GBK&#039;, &#039;gb18030&#039;, &#039;big5&#039;):<\/p>\n<p>    try:<\/p>\n<p>        content = response.content.decode(encoding)<\/p>\n<p>        break<\/p>\n<p>    except UnicodeDecodeError:<\/p>\n<p>        continue<\/p>\n<h2><strong>\u5982\u679c\u6240\u6709\u7f16\u7801\u90fd\u5931\u8d25\uff0c\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u6216\u8005\u8fd4\u56deNone<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>Python \u4e2d\u4e5f\u6709\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u548c\u8bc6\u522b\u7f16\u7801\u3002\u4f8b\u5982\uff0cBeautiful Soup \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u5728\u89e3\u6790 HTML \u6587\u6863\u65f6\u5e2e\u52a9\u81ea\u52a8\u5904\u7406\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#039;html.parser&#039;, from_encoding=&#039;GBK&#039;)<\/p>\n<h2><strong>\u83b7\u53d6\u89e3\u6790\u540e\u7684\u6587\u672c\u5185\u5bb9<\/strong><\/h2>\n<p>content = soup.get_text()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837BeautifulSoup\u4f1a\u5c1d\u8bd5\u4ee5\u6307\u5b9a\u7684GBK\u7f16\u7801\u6765\u89e3\u6790\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u7f16\u7801\u95ee\u9898\u540e\uff0c\u5982\u679c\u4f60\u8981\u4fdd\u5b58\u6587\u4ef6\uff0c\u4e5f\u9700\u8981\u6307\u5b9a\u7f16\u7801\u4e3aGBK\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#039;page.html&#039;, &#039;w&#039;, encoding=&#039;GBK&#039;) as f:<\/p>\n<p>    f.write(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4ed4\u7ec6\u5904\u7406\u7f16\u7801\u7684\u68c0\u6d4b\u548c\u8f6c\u6362\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5728 Python \u7684\u7f51\u7edc\u722c\u866b\u4e2d\u5904\u7406GBK\u7f16\u7801\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a\u5982\u4f55\u5728Python\u4e2d\u722c\u53d6\u7f51\u9875\u5e76\u8fd4\u56degbk\u7f16\u7801\u7684\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u5728\u4f7f\u7528Python\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7684\u662fUTF-8\u7f16\u7801\u7684\u5185\u5bb9\u3002\u5982\u679c\u4f60\u9700\u8981\u8fd4\u56degbk\u7f16\u7801\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7f51\u9875\u7684\u54cd\u5e94\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>content.decode(&#039;gbk&#039;)<\/code>\u5c06\u54cd\u5e94\u5185\u5bb9\u6309\u7167gbk\u7f16\u7801\u8fdb\u884c\u89e3\u7801\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\n\nurl = &quot;http:\/\/example.com&quot;\nresponse = requests.get(url)\nhtml_content = response.content.decode(&#039;gbk&#039;)\nprint(html_content)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u4ee5gbk\u7f16\u7801\u89e3\u7801\u540e\u7684\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u5728Python\u722c\u53d6\u65f6\u5904\u7406gbk\u7f16\u7801\u5b57\u7b26\u7684\u4e71\u7801\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u5728\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u65f6\uff0c\u6709\u65f6\u5019\u7531\u4e8e\u7f51\u9875\u4f7f\u7528\u7684\u662fgbk\u7f16\u7801\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u89e3\u7801\u51fa\u73b0\u4e71\u7801\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528chardet\u5e93\u6765\u81ea\u52a8\u68c0\u6d4b\u7f51\u9875\u5185\u5bb9\u7684\u7f16\u7801\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u7136\u540e\u4f7f\u7528chardet\u5e93\u7684<code>detect<\/code>\u65b9\u6cd5\u6765\u68c0\u6d4b\u5185\u5bb9\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u7136\u540e\u518d\u6839\u636e\u68c0\u6d4b\u7ed3\u679c\u9009\u62e9\u5408\u9002\u7684\u7f16\u7801\u8fdb\u884c\u89e3\u7801\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\nimport chardet\n\nurl = &quot;http:\/\/example.com&quot;\nresponse = requests.get(url)\nencoding = chardet.detect(response.content)[&#039;encoding&#039;]\nhtml_content = response.content.decode(encoding)\nprint(html_content)\n<\/code><\/pre>\n<p><strong>\u95ee\u98983\uff1a\u5982\u4f55\u5c06\u83b7\u53d6\u5230\u7684gbk\u7f16\u7801\u5185\u5bb9\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><\/p>\n<p>\u56de\u7b54\uff1a\u5728\u722c\u53d6\u7f51\u9875\u5e76\u83b7\u53d6\u5230gbk\u7f16\u7801\u5185\u5bb9\u4e4b\u540e\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u8fd9\u4e9b\u5185\u5bb9\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u4ee5\u4e8c\u8fdb\u5236\u5199\u5165\u7684\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<p>\u7136\u540e\uff0c\u5c06\u83b7\u53d6\u5230\u7684gbk\u7f16\u7801\u5185\u5bb9\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>write()<\/code>\u65b9\u6cd5\u6765\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">html_content = &quot;\u8fd9\u662f\u4e00\u4e2a\u4ee5gbk\u7f16\u7801\u7684\u5185\u5bb9&quot;\nwith open(&quot;output.txt&quot;, &quot;wb&quot;) as file:\n    file.write(html_content.encode(&#039;gbk&#039;))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u83b7\u53d6\u5230\u7684\u5185\u5bb9\u5c31\u4f1a\u4ee5gbk\u7f16\u7801\u5199\u5165\u5230\u6587\u4ef6\u4e2d\u4e86\u3002\u8bf7\u786e\u4fdd\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u4e0e\u5199\u5165\u7684\u5185\u5bb9\u7f16\u7801\u683c\u5f0f\u4e00\u81f4\uff0c\u4ee5\u514d\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"GBK \u7f16\u7801\u662f\u9488\u5bf9\u7b80\u4f53\u4e2d\u6587\u7684\u6269\u5c55\u5b57\u7b26\u96c6\uff0c\u800c\u5728\u4f7f\u7528 Python \u8fdb\u884c\u7f51\u7edc\u722c\u866b\u65f6\u5904\u7406GBK\u7f16\u7801\u901a\u5e38\u662f\u4e3a\u4e86\u6b63\u786e\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":173617,"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\/173604"}],"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=173604"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173604\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173617"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}