test_sui.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import httpx
  4. # 设置代理,包含协议部分
  5. proxies = {
  6. "http://": "http://127.0.0.1:7890",
  7. "https://": "http://127.0.0.1:7890",
  8. }
  9. # 设置请求头
  10. headers = {
  11. "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"
  12. }
  13. # 创建 httpx 客户端,并设置代理和请求头
  14. client = httpx.Client(proxies=proxies, headers=headers)
  15. # 目标 URL
  16. url = "https://coinmarketcap.com/currencies/x/"
  17. try:
  18. result = ''
  19. resp = client.get(url)
  20. page = resp.text
  21. text = re.findall('<strong>(.*?)sc-65e7f566-0', page)[0] if re.findall(
  22. '<strong>(.*?)sc-65e7f566-0', page) else 'No Data'
  23. text = re.sub(r'</strong>', '', text)
  24. text = re.sub(r'<!-- -->', '', text)
  25. text = re.sub(r'</p></div></div><div class="', '', text)
  26. print(text.replace('我们会实时更新SUI兑换为CNY的价格。 ', ''))
  27. prices = re.findall(r'\$(\d+\.\d+)', text)
  28. if prices:
  29. result += f'prices: ${prices[0]}\n'
  30. volumes = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  31. if volumes:
  32. result += f'24-hour trading volume: ${volumes[1]}\n'
  33. change = re.findall(r'(up|down) (\d+\.\d+)%', text)
  34. if change:
  35. c = ' '.join(change[0])
  36. result += f'change: {c}%\n'
  37. live_market_cap = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  38. if live_market_cap:
  39. if len(live_market_cap) == 3:
  40. result += f'live market cap: ${live_market_cap[2]}\n'
  41. else:
  42. pass
  43. max_circulating_supply = re.findall(r'(\d{1,3}(?:,\d{3})*)', text)
  44. if max_circulating_supply:
  45. result += f'max circulating supply: {max_circulating_supply[-1]}\n'
  46. result += '\n'
  47. print(result)
  48. except httpx.RequestError as e:
  49. # 打印错误信息
  50. print(f"请求错误: {e}")
  51. except Exception as e:
  52. # 打印其他错误信息
  53. print(f"发生错误: {e}")