Files
lm_code/test1.py
2025-11-12 17:37:39 +08:00

69 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
import requests
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'cache-control': 'no-cache',
'dnt': '1',
'origin': 'https://www.websea.com',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://www.websea.com/',
'sec-ch-ua': '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0',
}
# # 获取当前日期UTC
# today = datetime.datetime.now(datetime.timezone.utc).date()
#
# # 当天开始和结束时间
# start_of_day = datetime.datetime.combine(today, datetime.time.min, tzinfo=datetime.timezone.utc)
# end_of_day = datetime.datetime.combine(today, datetime.time.max, tzinfo=datetime.timezone.utc)
# 获取今天日期(本地时间,中国时区 UTC+8
tz = datetime.timezone(datetime.timedelta(hours=8))
today = datetime.datetime.now(tz).date()
# 当天开始时间(北京时间 00:00
start_of_day = datetime.datetime.combine(today, datetime.time.min, tzinfo=tz)
# 当天结束时间(北京时间 23:59:59.999999
end_of_day = datetime.datetime.combine(today, datetime.time.max, tzinfo=tz)
params = {
'symbol': 'BTC-USDT',
'period': '30min',
'start': int(start_of_day.timestamp()),
'end': int(end_of_day.timestamp()),
}
response = requests.get('https://eapi.websea.com/webApi/market/getKline', params=params, headers=headers)
data = response.json()["result"]["data"]
if not data:
print("没有获取到数据")
else:
# 当前价格 = 最新一条 K 线的 close
current_price = float(data[0]['close'])
# 当天最高价 = 当天所有 K 线的 high
today_high = max(float(item['high']) for item in data)
# 当天最低价 = 当天所有 K 线的 low
today_low = min(float(item['low']) for item in data)
# 24 小时涨幅计算(用最新一条 close 与 24 小时前的价格对比)
# 如果接口只能获取当天数据,可能不足 24 小时,可根据需求扩展
# 假设获取过去 24 小时数据,使用第一条 K 线的 open 作为 24 小时前价格
price_24h_ago = float(data[-1]['open'])
change_24h = (current_price - price_24h_ago) / price_24h_ago * 100
print(f"当前价格: {current_price}")
print(f"24小时涨幅: {change_24h:.2f}%")
print(f"当天最高价: {today_high}")
print(f"当天最低价: {today_low}")