Python urllib2 POST中文出错如何破解,,response = u
Python urllib2 POST中文出错如何破解,,response = u
response = urllib2.urlopen('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4966bd9df888d05c&corpsecret=cIr3USJQLxp7wrMYFiaHKgjE8Az3Y9xTEqGQ3vrTBMoYguIepPkta-Ik5RsdMpqN')res = response.read()str = json.loads(res)token = str['access_token']url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+token#print urlsendmsg = {"touser": "mr-zhoong", "toparty": "1", "totag": "1", "msgtype": "text", "agentid": "1", "text": {"content": u"你好This message is sent by co. ltd"}, "safe": "0" }data = json.dumps(sendmsg)req = urllib2.Request(url, data)response = urllib2.urlopen(req)the_page = response.read()print the_page
返回:{"errcode":40033,"errmsg":"invalid charset. please check your request, if include \uxxxx will create fail!"}
尝试过:data = json.dumps(sendmsg, ensure_ascii=False).encode('utf8')
#data = json.dumps(sendmsg).encode('gbk')
均返回错误
其中 "text": {"content": u"This message is sent by co. ltd"} 是可以成功的,返回:"errcode":0,"errmsg":"ok"
代码可以直接运行
建议使用requests库来做
使用requests
headers = {'Content-Type': 'application/json', "charset": "utf-8"}
json_string = simplejson.dumps(data, ensure_ascii=False).encode('utf8')
wechat_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'%self.get_access_token()
res = requests.post(wechat_url, data=json_string, headers=headers)
PS:使用requests的时候,参数使用data=,不要使用json=
这是我正在跑的代码。。。
编橙之家文章,
相关内容
- gevent.pool已设置并发数量,运行程序时却不受限制,,im
- Python导入方法都有哪些,除import外,pythonimport,我看见别
- 用python怎么不刷新网页而监控网页变化?,python监控
- python numba jit效率如何提高,numbajit,因为本人目前正在学
- 请问vmware如何从外部通过http访问内部的方法,vmware访问
- python语言为什么那么的参数函数和变量,python函数变量
- Python SDK安装遇到错误提示求助,pythonsdk,各位大牛们好
- Python2.6 pip安装gittle报错求解,python2.6gittle,python2.6 pi
- 需要用Python将编码转换成汉字求帮助,python汉字,请问,
- 关于python编辑器内提示的问题,python编辑器内提示,为什
评论关闭