You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
auto/test/test_coin.py

73 lines
2.0 KiB

# -*- coding: utf-8 -*-
import re
import httpx
# 设置代理,包含协议部分
proxies = {
"http://": "http://127.0.0.1:7890",
"https://": "http://127.0.0.1:7890",
}
# 设置请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107"
}
# 创建 httpx 客户端,并设置代理和请求头
client = httpx.Client(proxies=proxies, headers=headers)
# 目标 URL
# url = "https://coinmarketcap.com/currencies/sui/" # sui
url = "https://coinmarketcap.com/currencies/x/" # x
try:
result = ''
resp = client.get(url)
page = resp.text
text = re.findall('<strong>(.*?)sc-65e7f566-0', page)[0] if re.findall(
'<strong>(.*?)sc-65e7f566-0', page) else 'No Data'
text = re.sub(r'</strong>', '', text)
text = re.sub(r'<!-- -->', '', text)
text = re.sub(r'</p></div></div><div class="', '', text)
print(text.replace('我们会实时更新SUI兑换为CNY的价格。 ', ''))
prices = re.findall(r'\$(\d+\.\d+)', text)
if prices:
result += f'prices: ${prices[0]}\n'
volumes = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
if volumes:
result += f'24-hour trading volume: ${volumes[1]}\n'
change = re.findall(r'(up|down) (\d+\.\d+)%', text)
if change:
c = ' '.join(change[0])
result += f'change: {c}%\n'
live_market_cap = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
if live_market_cap:
if len(live_market_cap) == 3:
result += f'live market cap: ${live_market_cap[2]}\n'
else:
pass
max_circulating_supply = re.findall(r'(\d{1,3}(?:,\d{3})*)', text)
if max_circulating_supply:
result += f'max circulating supply: {max_circulating_supply[-1]}\n'
result += '\n'
print(result)
except httpx.RequestError as e:
# 打印错误信息
print(f"请求错误: {e}")
except Exception as e:
# 打印其他错误信息
print(f"发生错误: {e}")