| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # -*- coding: utf-8 -*-
- '''
- 使用 httpx 获取 coinmarketcap 最新数字币数据
- '''
- import httpx
- import re
- def get_coinmarketcap_coin_price(url, target):
- 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"
- }
- resp = httpx.get(url=url, headers=headers)
- if resp.status_code == 301:
- print(resp.text)
- exit(0)
- elif resp.status_code != 200:
- print(resp.status_code)
- exit(0)
- resp.encoding = 'utf-8'
- page = resp.text
- text = re.findall('<strong>(.*?)</p></div><div class="sc-65e7f566-0', page)[0] if re.findall(
- '<strong>(.*?)</p></div><div class="sc-65e7f566-0', page) else 'No Data'
- text = re.sub(r'</strong>', '', text)
- text = re.sub(r'<!-- -->', '', text)
- result = f'target: {target}\n'
- 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:
- result += f'live market cap: ${live_market_cap[2]}\n'
- 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]}'
- return result + '\n\n'
- if __name__ == '__main__':
- url_list = [
- # ['BTC', 'https://www.coinmarketcap.com/currencies/bitcoin/'],
- # ['ETH', 'https://www.coinmarketcap.com/currencies/ethereum/'],
- ['DOGE', 'https://coinmarketcap.com/currencies/dogecoin/'],
- ['ARB', 'https://coinmarketcap.com/currencies/arbitrum/'],
- ['ATH', 'https://coinmarketcap.com/currencies/aethir/'],
- ['SUI', 'https://coinmarketcap.com/currencies/sui/'],
- ]
- text = ''
- for data_list in url_list:
- text += get_coinmarketcap_coin_price(data_list[1], data_list[0])
- print(text)
|