Python 官方文档:入门教程 => 点击学习
解决方法: 先encode再quote。 原理: msg.encode('utf-8')是解决中文乱码问题。 quote():假如URL的 name 或者 value 值中有『&』、『%』或者『=』等符号,就会有问题。所以URL
解决方法:
先encode再quote。
原理:
msg.encode('utf-8')是解决中文乱码问题。
quote():假如URL的 name 或者 value 值中有『&』、『%』或者『=』等符号,就会有问题。所以URL中的参数字符串也需要把『&=』等符号进行编码,quote()就是对参数字符串中的『&=%』等符号进行编码。
例子:
# -*- coding: UTF-8 -*-
# python2.7
from urllib import quote
import requests
def HttpGet(sUrl):
header = {}
try:
response=requests.get(sUrl, headers=header)
sText = response.text
return sText
except BaseException:
print BaseException
def demo(msg):
sEncodeMsg = quote(msg.encode('utf-8'))
url = 'http://www.youdao.com/w/eng/' + sEncodeMsg
print httpGet (url)
demo(u'90%的数据')
--结束END--
本文标题: Python发送Http请求时,中文乱码
本文链接: https://www.lsjlt.com/news/188885.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0