{"id":1423,"date":"2022-11-23T16:52:56","date_gmt":"2022-11-23T08:52:56","guid":{"rendered":"http:\/\/blog.zhang.mba\/?p=1423"},"modified":"2022-11-23T23:21:31","modified_gmt":"2022-11-23T15:21:31","slug":"unicodeencodeerror-latin1-codec-cant-encode-charac","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2022\/11\/23\/16\/52\/56\/1423\/unicodeencodeerror-latin1-codec-cant-encode-charac\/python\/zhangzhiqi\/","title":{"rendered":"UnicodeEncodeError: \u2018latin-1\u2018 codec can\u2018t encode characters in position \u95ee\u9898"},"content":{"rendered":"<h2><a id=\"unicodeencodeerror%E2%80%98-latin-1%E2%80%99-codec-can%E2%80%99t-encode-characters-in-position-48-49-body%E2%80%98%E6%90%9E%E4%BA%8B%E6%83%85%E2%80%99-is-not-valid-latin-1-use-body-encode%E2%80%98-utf-8%E2%80%99-if-you-want-to-send-it-encoded-in-utf-8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>UnicodeEncodeError: \u2018latin-1\u2019 codec can\u2019t encode characters in position 48-49: Body (\u2018\u641e\u4e8b\u60c5\u2019) is not valid Latin-1. Use body.encode(\u2018utf-8\u2019) if you want to send it encoded in UTF-8<\/h2>\n<p>\u5728\u4f7f\u7528python\u6216\u8005robot framework \u5f15\u7528 <a href=\"https:\/\/so.csdn.net\/so\/search?q=requests&amp;spm=1001.2101.3001.7020\">requests<\/a>\u65f6\uff0c\u8bf7\u6c42\u53d1\u751f\u4ee5\u4e0b\u9519\u8bef\u63d0\u793a\uff1aUnicodeEncodeError: \u2018latin-1\u2019 codec can\u2019t encode characters in position 48-49: Body (\u2018\u641e\u4e8b\u60c5\u2019) is not valid Latin-1. Use body.encode(\u2018utf-8\u2019) if you want to send it encoded in UTF-8\uff1b<\/p>\n<h2><a id=\"%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u89e3\u51b3\u529e\u6cd5\uff1a<\/h2>\n<p>\u8bf7\u6c42\u7684\u62a5\u6587\u5305\u542b\u6709\u4e2d\u6587\u5b57\u7b26\u6216\u8005\u5176\u4ed6\u5b57\u7b26\u5bfc\u81f4\u7f16\u7801\u683c\u5f0f\u4e0d\u652f\u6301\uff0c\u53ef\u4ee5\u5c06\u8bf7\u6c42\u4f53\u7684\u7f16\u7801\u683c\u5f0f\u6539\u4e3a\u2018UTF-8\u2019<br \/>\n\u56e0\u4e3a\u6c49\u5b57\u65e0\u6cd5<a href=\"https:\/\/so.csdn.net\/so\/search?q=Unicode&amp;spm=1001.2101.3001.7020\">Unicode<\/a>\u7f16\u7801<br \/>\n\u5982\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import requests\n\nurl = 'www.baidu.com'\ndatas = '\u641e\u4e8b\u60c5'\nbytedatas = datas.encode('UTF-8')  #\u8f6c\u6362\u7f16\u7801\u683c\u5f0f\nres = requests.post(url, data=bytedatas, headers=headers)\n<\/code><\/pre>\n<p>\u53e6\u5916\u5728\u4f7f\u7528pymysql \u65f6\uff0c\u6ca1\u6709\u5b9a\u4e49\u7f16\u7801\u683c\u5f0f\uff0c\u5728\u6570\u636e\u5e93\u4e2d insert \u63d2\u5165\u6c49\u5b57\u65f6\u4e5f\u53ef\u80fd\u51fa\u73b0\u4ee5\u4e0a\u62a5\u9519<br \/>\n\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import pymysql\ndb = pymysql.connect(host='116.0.0.1', port=3306, user='root', passwd='666', db='mysql', charset='utf8')\n#\u5728\u8fde\u63a5\u4e2d \u6307\u5b9a charset='utf8' \n\n<\/code><\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>UnicodeEncodeError: \u2018latin-1\u2019 codec can\u2019t encode characters in position 48-49: Body (\u2018\u641e\u4e8b\u60c5\u2019) is not valid Latin-1. Use body.encode(\u2018utf-8\u2019) if you want to send it encoded in UTF-8<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1423"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1423"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}