# -*- coding: utf-8 -*- ''' 使用 httpx 获取 coinmarketcap 最新数字币数据 ''' import time import os import sys import httpx import re sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto')) from utils.utils_send_gotify import * def get_coinmarketcap_coin_price(): url_list = [ ['BTC', 'https://coinmarketcap.com/currencies/bitcoin/'], ['ETH', 'https://coinmarketcap.com/currencies/ethereum/'], ['SOL', 'https://coinmarketcap.com/currencies/solana/'], ['GRASS', 'https://coinmarketcap.com/currencies/grass/'], ['SUI', 'https://coinmarketcap.com/currencies/sui/'], ['DOGE', 'https://coinmarketcap.com/currencies/dogecoin/'], ['ARB', 'https://coinmarketcap.com/currencies/arbitrum/'], ['ATH', 'https://coinmarketcap.com/currencies/aethir/'] ] 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" } result = '' for data_list in url_list: url = data_list[1] target = data_list[0] try: resp = httpx.get(url=url, headers=headers) except Exception as e: print(f'target: {target}, {e}') time.sleep(5) continue result += f'{target}\n' if resp.status_code == 301: print(resp.text) return None elif resp.status_code != 200: print(resp.status_code) return None resp.encoding = 'utf-8' page = resp.text text = re.findall('(.*?)sc-65e7f566-0', page)[0] if re.findall( '(.*?)sc-65e7f566-0', page) else 'No Data' text = re.sub(r'', '', text) text = re.sub(r'', '', text) text = re.sub(r'