直接上筛选符合条件币代码
import ccxt
MEXC_CONFIG = {
'proxies': {'http': '', 'https': '', }, # 设置vpn
}
exchange = ccxt.mexc(MEXC_CONFIG)
def MEXC_XB():
'''
:return: {'symbol': 'DC_USDT', 'riseFallRate': 9.64, 'lastPrice': '0.000807', 'fundingRate': -0.01, 'amount24': 1.04}
'''
try:
data_list = exchange.contractPublicGetTicker()['data']
ticker_list = [] # 提取所需要的数据
for i in data_list:
tickers = {}
tickers['symbol'] = i['symbol'] # 币种
tickers['riseFallRate'] = round(float(i['riseFallRate'])*100,3) # 涨跌幅
tickers['lastPrice'] = i['lastPrice'] # 最新价格
tickers['fundingRate'] = round(float(i['fundingRate'])*100,4) # 资金费率
tickers['amount24'] = round(float(i['amount24'])/1000000,3) # 24小时成交额
ticker_list.append(tickers)
funding_rise = [] # 选出资金费率>0 和 涨跌幅> 0的数据
for i in ticker_list:
if i['amount24'] > 0: # 24小时成交额大于5M
if float(i['fundingRate']) > -0.011 : #资金费率
if float(i['riseFallRate']) > 0: # 涨跌幅
funding_rise.append(i)
funding_rise.sort(key=lambda s: float(s["riseFallRate"]),reverse=True) # 根据涨跌幅进行排序,按照降序排
# funding_rise.sort(key=lambda s: float(s["fundingRate"]),reverse=True) # 根据涨跌幅进行排序,按照降序排
for i in funding_rise[:1]:
return i
except Exception as e:
print('选币错误',e)
对微信公众号发送信息(这里用的测试号,毕竟只有自己使用)
import requests
import json
import time
from Vx_push.XB import *
appID = ''
appsecret = ''
template_id = '' # 模版ID
dataJson = {"symbol": MEXC_XB()['symbol'] ,
"riseFallRate": MEXC_XB()['riseFallRate'],
"lastPrice": MEXC_XB()['lastPrice'],
"fundingRate": MEXC_XB()['fundingRate'],
"amount24": MEXC_XB()['amount24'],
}
def get_access_token():
"""
获取access_token
通过查阅微信公众号的开发说明就清晰明了了
"""
url = f'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appID}&secret={appsecret}'
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'
}
response = requests.get(url, headers=headers).json()
access_token = response.get('access_token')
return access_token
def get_openid(access_token):
"""
获取所有用户的openid 关注中的微信号
微信公众号开发文档中可以查阅获取openid的方法
"""
next_openid = ''
url_openid = f'https://api.weixin.qq.com/cgi-bin/user/get?access_token={access_token}&next_openid={next_openid}'
ans = requests.get(url_openid)
open_ids = json.loads(ans.content)['data']['openid']
return open_ids
def sendmsg(access_token,open_ids):
"""
给所有用户发送消息
"""
url = f"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}"
if open_ids != '':
for open_id in open_ids:
body = {
"touser": open_id,
"template_id": template_id,
# 对应模板中的数据模板
"data": {
"time":{"value":time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())},
"symbol": {
"value": dataJson.get("symbol"),
},
"riseFallRate": {
"value": dataJson.get("riseFallRate"),
},
"lastPrice": {
"value": dataJson.get("lastPrice"),
},
"fundingRate": {
"value": dataJson.get("fundingRate"),
},
"amount24": {
"value": dataJson.get("amount24"),
}
}
}
data = bytes(json.dumps(body, ensure_ascii=False).encode('utf-8')) # 将数据编码json并转换为bytes型
response = requests.post(url, data=data)
result = response.json() # 将返回信息json解码
if result['errmsg'] == 'ok':
print('发送成功,请确认微信是否成功') # 根据response查看是否广播成功
else:
print("当前没有用户关注该公众号!")
def run(m):
if float(MEXC_XB()['riseFallRate']) >20 :
access_token = get_access_token()
open_ids = get_openid(access_token,)
sendmsg(access_token, open_ids)
if __name__ == '__main__':
m = 1
while True:
try:
run(m)
time.sleep(m * 60) # 强制暂停1分钟
except Exception as e:
print('出错',e)
大概就这样。创建公众号时引用了:
https://blog.csdn.net/weixin_64200441/article/details/126953445
感谢这位博主