如何将多个请求合并成一个
本帖最后由 如默 于 2022-11-4 16:55 编辑先说一下,这个代码是网上看到用来给女友发送微信模板消息的,我调用了一个星座运势的接口,但是,不知道怎么把数据统一放在一个地方,然后调用这个。
目前代码比较笨,是请求多次,然后返回自己需要的数据,很浪费资源,能否修改一下,非常感谢!
多说一句:1,最重要是将多个请求合并成一个,且保留全部功能;2,能否将type参数后的这个星座名称设置成参数,这样方便修改,要不然需要修改很多个,比较麻烦
import requests
import json
import random
def httpGet(url, params):
r = requests.get(url, params)
return json.loads(r.content)
def httpPost(url, params):
r = requests.post(url, params)
return json.loads(r.content)
# 获取微信token
def getAccessToken(appId, appSecret):
params = {
'grant_type': 'client_credential',
'appid': appId,
'secret': appSecret
}
url = 'https://api.weixin.qq.com/cgi-bin/token'
return httpGet(url, params)
# 获取运势标题
def getFortuneTitle():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['title']
# 获取幸运颜色
def getLuckyColor():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['luckycolor']
# 获取幸运数字
def getLuckyNumber():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['luckynumber']
# 获取速配星座
def getLuckyConstellation():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['luckyconstellation']
# 获取短评
def getShortComment():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['shortcomment']
# 获取综合运势
def getFortuneText():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['fortunetext']['all']
# 获取爱情运势
def getFortuneLove():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['fortunetext']['love']
# 获取学业运势
def getFortuneWork():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['fortunetext']['work']
# 获取财富运势
def getFortuneMoney():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['fortunetext']['money']
# 获取健康运势
def getFortuneHealth():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk['data']['fortunetext']['health']
# 发送模版消息
def sendTemplateMessage(content, accessToken):
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + accessToken
return httpPost(url, content)
# 获取爱称
def getNickName():
nameHub = ['安琪','安琪宝贝','Angel','安安','琪琪','琪琪子','亲爱的安琪','心爱的安琪']
currentName = random.choice(nameHub)
return currentName
if __name__ == '__main__':
# 微信公众号的appId和appSecret
appId = 'xxx'
appSecret = 'xxx'
# 要发送人的openId列表
openIdList = ['xxx','xxx']
# 模版Id
templateId = 'xxx'
accessTokenInfo = getAccessToken(appId, appSecret)
accessToken = accessTokenInfo['access_token']
name = getNickName()
fortuneTitle = getFortuneTitle()
luckyColor = getLuckyColor()
luckyNumber = getLuckyNumber()
luckyConstellation = getLuckyConstellation()
shortComment = getShortComment()
fortuneText = getFortuneText()
fortuneLove = getFortuneLove()
fortuneWork = getFortuneWork()
fortuneMoney = getFortuneMoney()
fortuneHealth = getFortuneHealth()
for i in range(len(openIdList)):
data = {
'touser': openIdList,
'template_id': templateId,
'topcolor' : '#FF0000',
'data': {
'name':{
'value': name,
'color': '#ee3f4d'
},
'fortuneTitle':{
'value': fortuneTitle,
'color': '#282c34'
},
'luckyColor':{
'value': luckyColor,
'color': '#065279'
},
'luckyNumber':{
'value': luckyNumber,
'color': '#4b5cc4'
},
'luckyConstellation':{
'value': luckyConstellation,
'color': '#e06c75'
},
'shortComment':{
'value': shortComment,
'color':'#4c8dae'
},
'fortuneText':{
'value': fortuneText,
'color': '#db5a6b'
},
'fortuneLove':{
'value': fortuneLove,
'color': '#801dae'
},
'fortuneWork':{
'value': fortuneWork,
'color': '#21a675'
},
'fortuneMoney':{
'value': fortuneMoney,
'color': '#f2be45'
},
'fortuneHealth':{
'value': fortuneHealth,
'color': '#b36d61'
}
}
}
params = json.dumps(data)
print(sendTemplateMessage(params, accessToken))
粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据就在一起的,现在你反而把它分开来写了,感觉这不是弄复杂了吗? talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data']
这样就把所有的数据都放在data里面,想用哪个取哪个 cjh4312@163.com 发表于 2022-11-4 17:09
粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据 ...
所以要怎么改呢 cjh4312@163.com 发表于 2022-11-4 17:15
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data']
...
能写完整吗,我试了一下不行啊 如默 发表于 2022-11-4 17:35
能写完整吗,我试了一下不行啊
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data']
print(data)
出来的数据就是这样的,这不正是你要的。
{'title': '白羊座今日运势', 'time': '11月4日', 'fortune': {'all': 4, 'love': 4, 'work': 4, 'money': 4}, 'index': {'health': '93%', 'discuss': '90%'}, 'luckycolor': '蓝色', 'luckynumber': '2', 'luckyconstellation': '天蝎座', 'shortcomment': '抓住出击的机会', 'fortunetext': {'all': '运势正当旺,敢于抓住主动出击的机会。很容易接收到来自外界的正能量,会让你瞬间就像打了鸡血一样倍有能量。生活方面也能以阳光积极的精神感染着身边的人,拥有着满满的活力,做每一件事都很认真。', 'love': '单身的犹如全世界在助攻,表现机会很多。恋爱中的保持舒服的相处空间,感情稳定发展。', 'work': '越有挑战系数的目标越能激发起胜负欲,也愿意付出更多的努力,不辞劳苦的一天。', 'money': '求财平平顺顺,对赚钱的机会嗅觉灵敏,敢于进行尝试,并且拥有进账机会。', 'health': '需要警惕用手挤痘痘的行为,尤其是脸部三角区的痘痘更不能挤,容易引起严重后果。'}} cjh4312@163.com 发表于 2022-11-4 17:44
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data' ...
明白了,你这个是没用我开头封装的httoget,其实已经请求到了
# 获取运势信息
def getFortuneData():
url = 'https://api.vvhan.com/api/horoscope'
data = httpGet(url, {'type': 'sagittarius','time':'today'})
return data
在这样写
fortuneData = getFortuneData()
fortuneTitle = fortuneData['data']['title']
luckyColor = fortuneData['data']['luckycolor']
luckyNumber = fortuneData['data']['luckynumber']
luckyConstellation = fortuneData['data']['luckyconstellation']
shortComment = fortuneData['data']['shortcomment']
fortuneText = fortuneData['data']['fortunetext']['all']
fortuneLove = fortuneData['data']['fortunetext']['love']
fortuneWork = fortuneData['data']['fortunetext']['work']
fortuneMoney = fortuneData['data']['fortunetext']['money']
fortuneHealth = fortuneData['data']['fortunetext']['health']
就好了,只用请求一次,参数也只用在这一个地方修改 学习学习~ {:5_108:} 学习学习~ def getFortuneHealth():
url = 'https://api.vvhan.com/api/horoscope'
talk = httpGet(url, {'type': 'aries','time':'today'})
return talk
这样就行了 {:5_102:}
页:
[1]