python3 urllib post json

首先给大家推荐一下我老师大神的人工智能教学网站。教学不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵黄段子!点这里可以跳转到网站

用python django做微信开发,设置用户分组时,如下代码出现错误:

TypeError: POST data should be bytes or an iterable of bytes. It cannot be of ty
pe str.

url='https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token='+access_tokendata={'openid':str(openid),'to_groupid':str(groupid)}data = json.dumps(data)request=urllib.request.Request(url)result=urllib.request.urlopen(request,data).read()print(result)

解决办法,加入:data=bytes(data,’utf8′)

url='https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token='+access_tokendata={'openid':str(openid),'to_groupid':str(groupid)}data = json.dumps(data)data=bytes(data,'utf8')request=urllib.request.Request(url)result=urllib.request.urlopen(request,data).read()print(result)

点这里可以跳转到人工智能网站

发表评论