python-post-json 中文问题---微信公众号中遇到的问题

4 篇文章 0 订阅
转载自http://my.oschina.net/yangyanxing/blog/159211
import
 urllib2
02 import json
03  
04 html = urllib2.urlopen(r'http://api.douban.com/v2/book/isbn/9787218087351')
05  
06 hjson = json.loads(heml.read())
07  
08 print hjson['rating']
09 print hjson['images']['large']
10 print hjson['summary']



python中json格式数据的编码和解码

http://www.01happy.com/python-json-encode-and-decode/

python编程_python通过get方式,post方式发送http请求和接收http响应_import urllib模块,import urllib2模块,import httplib模块  

http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201231085444250/



转载自http://stackoverflow.com/questions/9746303/how-do-i-send-a-post-request-as-a-json
通过python 发送post请求,并附带json信息
  
  
requrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s" %access_token
#之前的错误是因为在替换%s的时候,后面使用了逗号,以至于requrl从字符串变成了数组
#所以提示AttributeError: 'tuple' object has no attribute 'strip'

req = urllib2.Request(requrl)
req.add_header('Content-Type', 'application/json')
#重新加载系统默认的编码方式
reload(sys)
sys.setdefaultencoding('utf-8')
response = urllib2.urlopen(req, json.dumps(body,ensure_ascii=False))
#json.dumps会自动把中文转换为unicode,然后提交后,会回复{"errcode":40033,"errmsg":"invalid charset. please check your reques#t, if include \\uxxxx will create fail!"},所以只需要添加ensure_ascii=False,就可以成功添加

res = response.read()
print res

Python下调用json.dumps中文显示问题解决办法


json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如:

>>> import json
>>> js = json.loads('{"haha": "哈哈"}')
>>> print json.dumps(js)
{"haha": "\u54c8\u54c8"}

解决办法很简单:

>>> print json.dumps(js, ensure_ascii=False)   
{"haha": "哈哈"} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python可以通过企业微信开放的接口实现消息推送功能。 首先,需要在企业微信后台注册应用并获取应用的相关参数,比如应用ID、应用密钥等。然后,使用Python的requests库发送HTTP请求来调用企业微信提供的消息推送接口。 可以使用以下代码示例通过企业微信发送文本消息: ```python import requests import json # 获取企业微信应用的相关信息 corpid = '企业ID' corpsecret = '应用密钥' agentid = '应用ID' # 获取access_token url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}' response = requests.get(url) access_token = json.loads(response.text)['access_token'] # 构造消息内容 message = { 'touser': '@all', # 发送给所有用户 'msgtype': 'text', # 发送文本消息 'agentid': agentid, 'text': { 'content': '这是一条测试消息' # 消息内容 } } # 发送消息 send_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}' response = requests.post(send_url, data=json.dumps(message)) result = json.loads(response.text) if result['errcode'] == 0: print('消息发送成功') else: print('消息发送失败') ``` 上述代码首先获取了access_token,然后构造了一个文本消息的消息体,包含消息内容和发送对象(这里使用@all表示发送给所有用户)。最后,通过POST请求将消息发送给企业微信。 在实际使用,可以根据需要自定义消息内容、发送对象等相关参数。此外,企业微信还支持推送图片、文件、图文等多种类型的消息,开发者可以按需进行配置。 以上是使用Python实现通过企业微信进行消息推送的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值