本文主要是介绍解决python中requests请求时报错:UnicodeEncodeError: ‘latin-1‘ codec can‘t encode character,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
出问题的代码:
import requests
from fake_useragent import UserAgentmy_cookie = ("cna=DbGFGMr/uwYCAWdVkMXhdvE2; t=345dcf6624b93228c09a5d0b4b9c39f""d; tracknick=\u5317\u5BAB\u4F9D\u4E91; enc=rk/rlSa2hHpBpCAzJO/j0qYSuvIH42Lx2Eud6y""jHN1aw6qZ6zmc9JOVYtqziSI5NiAso56MjAHJyruChJ07X4Q==; hng=CN|zh-CN|CNY|156; thw=cn; xlly_""s=1; _m_h5_tk=babc8478350536617322592455c4d094_1615717600679; _m_h5_tk_enc=d0a8f738ef2a6c""de588fba11b373a8b6; _samesite_flag_=true; cookie2=1a7ec170b2ee23c299e87450d3282c1c; _""tb_token_=ee7e45b8b7333; sgcookie=E1008thWVAyphHncHvj4cXLVsJ+ScG2w2q1jkccyPm/OjJY3hq""rZcFGDMDl4zKMa0g2EFPCO9XVtb+X9RP7Lx9Tv2Q==; unb=2531730423; uc3=id2=UU2xpBe7weEo1w==&vt3=F8dC""uAotVo3wrKOTkfc=&lg2=UIHiLt3xD8xYTw==&nk2=0vR/Q6SVOBU=; csg=1c749ce2; lgc=\u5317\u5BAB\u4F9D\u4E91; co""okie17=UU2xpBe7weEo1w==; dnk=\u5317\u5BAB\u4F9D\u4E91; skt=c9aa7ac951278763; existShop=MTYxNTcwNz""E2Nw==; uc4=nk4=0@0EUJaZoazKl8EvNwnMnVx3St/A==&id4=0@U2/2sPN64kbhCNQ3t/eOeGIaz+ny; _cc_=UtA""SsssmfA==; _l_g_=Ug==; sg=云3f; _nk_=\u5317\u5BAB\u4F9D\u4E91; cookie1=BxpQEHc4LxaS7F1B0gdWV2hud5tsyX6""un5CDETGxEE8=; mt=ci=6_1; uc1=pas=0&cookie16=VT5L2FSpNgq6fDudInPRgavC+Q==&existShop=false&cookie14=Uoe1h""x5b5m1HbQ==&cookie21=U+GCWk/7p4mBoUyS4E9C&cookie15=W5iHLLyFOGW7aA==; tfstk=cNGRBu2SFnxlqCUx7YpcRmKOiclRaG_""8xaZd9UnH3OSXkZ1dOsqEsfH_ILaF2SLA.; l=eBjyU-_PjEyJ8nJBBOfanurza77OSIRYmuPzaNbMiOCP_85e5HjOW6NBlN8wC3hVh6EMR35""VLnkHBeYBqQOSnxvtIosM_Ckmn; isg=BMnJL4VEzTUrjbEtlz3tLZFy2PUjFr1I2BYwIGs-bbDvsujEs2KTGfHq8RYE7lWA")ua = UserAgent()headers = {'User-Agent': ua.random,'Cookie': my_cookie}url = "https://i.taobao.com/my_taobao.htm?spm=a21bo.2017.1997525045.1.5af911d9JXYiUD"data = requests.post(url, headers=headers)
print(data.text)
修改后的代码:
import requests
from fake_useragent import UserAgentmy_cookie = ("cna=DbGFGMr/uwYCAWdVkMXhdvE2; t=345dcf6624b93228c09a5d0b4b9c39f""d; tracknick=\u5317\u5BAB\u4F9D\u4E91; enc=rk/rlSa2hHpBpCAzJO/j0qYSuvIH42Lx2Eud6y""jHN1aw6qZ6zmc9JOVYtqziSI5NiAso56MjAHJyruChJ07X4Q==; hng=CN|zh-CN|CNY|156; thw=cn; xlly_""s=1; _m_h5_tk=babc8478350536617322592455c4d094_1615717600679; _m_h5_tk_enc=d0a8f738ef2a6c""de588fba11b373a8b6; _samesite_flag_=true; cookie2=1a7ec170b2ee23c299e87450d3282c1c; _""tb_token_=ee7e45b8b7333; sgcookie=E1008thWVAyphHncHvj4cXLVsJ+ScG2w2q1jkccyPm/OjJY3hq""rZcFGDMDl4zKMa0g2EFPCO9XVtb+X9RP7Lx9Tv2Q==; unb=2531730423; uc3=id2=UU2xpBe7weEo1w==&vt3=F8dC""uAotVo3wrKOTkfc=&lg2=UIHiLt3xD8xYTw==&nk2=0vR/Q6SVOBU=; csg=1c749ce2; lgc=\u5317\u5BAB\u4F9D\u4E91; co""okie17=UU2xpBe7weEo1w==; dnk=\u5317\u5BAB\u4F9D\u4E91; skt=c9aa7ac951278763; existShop=MTYxNTcwNz""E2Nw==; uc4=nk4=0@0EUJaZoazKl8EvNwnMnVx3St/A==&id4=0@U2/2sPN64kbhCNQ3t/eOeGIaz+ny; _cc_=UtA""SsssmfA==; _l_g_=Ug==; sg=云3f; _nk_=\u5317\u5BAB\u4F9D\u4E91; cookie1=BxpQEHc4LxaS7F1B0gdWV2hud5tsyX6""un5CDETGxEE8=; mt=ci=6_1; uc1=pas=0&cookie16=VT5L2FSpNgq6fDudInPRgavC+Q==&existShop=false&cookie14=Uoe1h""x5b5m1HbQ==&cookie21=U+GCWk/7p4mBoUyS4E9C&cookie15=W5iHLLyFOGW7aA==; tfstk=cNGRBu2SFnxlqCUx7YpcRmKOiclRaG_""8xaZd9UnH3OSXkZ1dOsqEsfH_ILaF2SLA.; l=eBjyU-_PjEyJ8nJBBOfanurza77OSIRYmuPzaNbMiOCP_85e5HjOW6NBlN8wC3hVh6EMR35""VLnkHBeYBqQOSnxvtIosM_Ckmn; isg=BMnJL4VEzTUrjbEtlz3tLZFy2PUjFr1I2BYwIGs-bbDvsujEs2KTGfHq8RYE7lWA").encode("utf-8").decode("latin1")ua = UserAgent()headers = {'User-Agent': ua.random,'Cookie': my_cookie}url = "https://i.taobao.com/my_taobao.htm?spm=a21bo.2017.1997525045.1.5af911d9JXYiUD"data = requests.post(url, headers=headers)
print(data.text)
在请求数据的的时候注意中文编码
‘你的字符串’.encode( “utf-8”).decode(“latin1”)
这篇关于解决python中requests请求时报错:UnicodeEncodeError: ‘latin-1‘ codec can‘t encode character的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!