鱼C论坛

 找回密码
 立即注册
查看: 2942|回复: 11

[已解决]如何将多个请求合并成一个

[复制链接]
发表于 2022-11-4 16:38:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 如默 于 2022-11-4 16:55 编辑

先说一下,这个代码是网上看到用来给女友发送微信模板消息的,我调用了一个星座运势的接口,但是,不知道怎么把数据统一放在一个地方,然后调用这个。
目前代码比较笨,是请求多次,然后返回自己需要的数据,很浪费资源,能否修改一下,非常感谢!
多说一句:1,最重要是将多个请求合并成一个,且保留全部功能;2,能否将type参数后的这个星座名称设置成参数,这样方便修改,要不然需要修改很多个,比较麻烦

  1. import requests
  2. import json
  3. import random

  4. def httpGet(url, params):
  5.     r = requests.get(url, params)
  6.     return json.loads(r.content)

  7. def httpPost(url, params):
  8.     r = requests.post(url, params)
  9.     return json.loads(r.content)

  10. # 获取微信token
  11. def getAccessToken(appId, appSecret):
  12.     params = {
  13.         'grant_type': 'client_credential',
  14.         'appid': appId,
  15.         'secret': appSecret
  16.     }
  17.     url = 'https://api.weixin.qq.com/cgi-bin/token'
  18.     return httpGet(url, params)

  19. # 获取运势标题
  20. def getFortuneTitle():
  21.     url = 'https://api.vvhan.com/api/horoscope'
  22.     talk = httpGet(url, {'type': 'aries','time':'today'})
  23.     return talk['data']['title']

  24. # 获取幸运颜色
  25. def getLuckyColor():
  26.     url = 'https://api.vvhan.com/api/horoscope'
  27.     talk = httpGet(url, {'type': 'aries','time':'today'})
  28.     return talk['data']['luckycolor']

  29. # 获取幸运数字
  30. def getLuckyNumber():
  31.     url = 'https://api.vvhan.com/api/horoscope'
  32.     talk = httpGet(url, {'type': 'aries','time':'today'})
  33.     return talk['data']['luckynumber']

  34. # 获取速配星座
  35. def getLuckyConstellation():
  36.     url = 'https://api.vvhan.com/api/horoscope'
  37.     talk = httpGet(url, {'type': 'aries','time':'today'})
  38.     return talk['data']['luckyconstellation']

  39. # 获取短评
  40. def getShortComment():
  41.     url = 'https://api.vvhan.com/api/horoscope'
  42.     talk = httpGet(url, {'type': 'aries','time':'today'})
  43.     return talk['data']['shortcomment']

  44. # 获取综合运势
  45. def getFortuneText():
  46.     url = 'https://api.vvhan.com/api/horoscope'
  47.     talk = httpGet(url, {'type': 'aries','time':'today'})
  48.     return talk['data']['fortunetext']['all']

  49. # 获取爱情运势
  50. def getFortuneLove():
  51.     url = 'https://api.vvhan.com/api/horoscope'
  52.     talk = httpGet(url, {'type': 'aries','time':'today'})
  53.     return talk['data']['fortunetext']['love']

  54. # 获取学业运势
  55. def getFortuneWork():
  56.     url = 'https://api.vvhan.com/api/horoscope'
  57.     talk = httpGet(url, {'type': 'aries','time':'today'})
  58.     return talk['data']['fortunetext']['work']

  59. # 获取财富运势
  60. def getFortuneMoney():
  61.     url = 'https://api.vvhan.com/api/horoscope'
  62.     talk = httpGet(url, {'type': 'aries','time':'today'})
  63.     return talk['data']['fortunetext']['money']

  64. # 获取健康运势
  65. def getFortuneHealth():
  66.     url = 'https://api.vvhan.com/api/horoscope'
  67.     talk = httpGet(url, {'type': 'aries','time':'today'})
  68.     return talk['data']['fortunetext']['health']

  69. # 发送模版消息
  70. def sendTemplateMessage(content, accessToken):
  71.     url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + accessToken
  72.     return httpPost(url, content)

  73. # 获取爱称
  74. def getNickName():
  75.     nameHub = ['安琪','安琪宝贝','Angel','安安','琪琪','琪琪子','亲爱的安琪','心爱的安琪']
  76.     currentName = random.choice(nameHub)
  77.     return currentName

  78. if __name__ == '__main__':
  79.     # 微信公众号的appId和appSecret
  80.     appId = 'xxx'
  81.     appSecret = 'xxx'
  82.     # 要发送人的openId列表
  83.     openIdList = ['xxx','xxx']
  84.     # 模版Id
  85.     templateId = 'xxx'

  86.     accessTokenInfo = getAccessToken(appId, appSecret)
  87.     accessToken = accessTokenInfo['access_token']
  88.     name = getNickName()
  89.     fortuneTitle = getFortuneTitle()
  90.     luckyColor = getLuckyColor()
  91.     luckyNumber = getLuckyNumber()
  92.     luckyConstellation = getLuckyConstellation()
  93.     shortComment = getShortComment()
  94.     fortuneText = getFortuneText()
  95.     fortuneLove = getFortuneLove()
  96.     fortuneWork = getFortuneWork()
  97.     fortuneMoney = getFortuneMoney()
  98.     fortuneHealth = getFortuneHealth()


  99.     for i in range(len(openIdList)):
  100.         data = {
  101.             'touser': openIdList[i],
  102.             'template_id': templateId,
  103.             'topcolor' : '#FF0000',
  104.             'data': {
  105.                 'name':{
  106.                     'value': name,
  107.                     'color': '#ee3f4d'
  108.                 },
  109.                 'fortuneTitle':{
  110.                     'value': fortuneTitle,
  111.                     'color': '#282c34'
  112.                 },
  113.                 'luckyColor':{
  114.                     'value': luckyColor,
  115.                     'color': '#065279'
  116.                 },
  117.                 'luckyNumber':{
  118.                     'value': luckyNumber,
  119.                     'color': '#4b5cc4'
  120.                 },
  121.                 'luckyConstellation':{
  122.                     'value': luckyConstellation,
  123.                     'color': '#e06c75'
  124.                 },
  125.                 'shortComment':{
  126.                     'value': shortComment,
  127.                     'color':'#4c8dae'
  128.                 },
  129.                 'fortuneText':{
  130.                     'value': fortuneText,
  131.                     'color': '#db5a6b'
  132.                 },
  133.                 'fortuneLove':{
  134.                     'value': fortuneLove,
  135.                     'color': '#801dae'
  136.                 },
  137.                 'fortuneWork':{
  138.                     'value': fortuneWork,
  139.                     'color': '#21a675'
  140.                 },
  141.                 'fortuneMoney':{
  142.                     'value': fortuneMoney,
  143.                     'color': '#f2be45'
  144.                 },
  145.                 'fortuneHealth':{
  146.                     'value': fortuneHealth,
  147.                     'color': '#b36d61'
  148.                 }
  149.             }
  150.         }
  151.         params = json.dumps(data)

  152.         print(sendTemplateMessage(params, accessToken))
复制代码
最佳答案
2022-11-4 17:09:39
粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据就在一起的,现在你反而把它分开来写了,感觉这不是弄复杂了吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-4 17:09:39 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +10 鱼币

粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据就在一起的,现在你反而把它分开来写了,感觉这不是弄复杂了吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-4 17:15:03 | 显示全部楼层
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data']
这样就把所有的数据都放在data里面,想用哪个取哪个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-4 17:22:06 | 显示全部楼层
cjh4312@163.com 发表于 2022-11-4 17:09
粗略看了一下你写的,说说我的第一感觉。
不用写那么多的函数啊,返回的本来就是一个字典形式的。本来数据 ...

所以要怎么改呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-4 17:35:03 | 显示全部楼层
cjh4312@163.com 发表于 2022-11-4 17:15
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data']
...

能写完整吗,我试了一下不行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-4 17:44:22 | 显示全部楼层
如默 发表于 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': '需要警惕用手挤痘痘的行为,尤其是脸部三角区的痘痘更不能挤,容易引起严重后果。'}}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-4 17:51:34 | 显示全部楼层
cjh4312@163.com 发表于 2022-11-4 17:44
talk = requests.get(url, {'type': 'aries','time':'today'})
data=json.loads(talk.content)['data' ...

明白了,你这个是没用我开头封装的httoget,其实已经请求到了
  1. # 获取运势信息
  2. def getFortuneData():
  3.     url = 'https://api.vvhan.com/api/horoscope'
  4.     data = httpGet(url, {'type': 'sagittarius','time':'today'})
  5.     return data
复制代码


在这样写
  1.     fortuneData = getFortuneData()
  2.     fortuneTitle = fortuneData['data']['title']
  3.     luckyColor = fortuneData['data']['luckycolor']
  4.     luckyNumber = fortuneData['data']['luckynumber']
  5.     luckyConstellation = fortuneData['data']['luckyconstellation']
  6.     shortComment = fortuneData['data']['shortcomment']
  7.     fortuneText = fortuneData['data']['fortunetext']['all']
  8.     fortuneLove = fortuneData['data']['fortunetext']['love']
  9.     fortuneWork = fortuneData['data']['fortunetext']['work']
  10.     fortuneMoney = fortuneData['data']['fortunetext']['money']
  11.     fortuneHealth = fortuneData['data']['fortunetext']['health']
复制代码


就好了,只用请求一次,参数也只用在这一个地方修改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-4 19:14:15 | 显示全部楼层

回帖奖励 +10 鱼币

学习学习~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-5 00:04:01 | 显示全部楼层

回帖奖励 +10 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-5 07:03:43 | 显示全部楼层
学习学习~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-5 08:07:52 | 显示全部楼层

回帖奖励 +10 鱼币

def getFortuneHealth():
    url = 'https://api.vvhan.com/api/horoscope'
    talk = httpGet(url, {'type': 'aries','time':'today'})
    return talk
这样就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-5 08:21:46 | 显示全部楼层

回帖奖励 +10 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 00:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表