test_coin.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/sui/" # sui
  17. url = "https://coinmarketcap.com/currencies/x/" # x
  18. try:
  19. result = ''
  20. resp = client.get(url)
  21. page = resp.text
  22. text = re.findall('<strong>(.*?)sc-65e7f566-0', page)[0] if re.findall(
  23. '<strong>(.*?)sc-65e7f566-0', page) else 'No Data'
  24. text = re.sub(r'</strong>', '', text)
  25. text = re.sub(r'<!-- -->', '', text)
  26. text = re.sub(r'</p></div></div><div class="', '', text)
  27. print(text.replace('我们会实时更新SUI兑换为CNY的价格。 ', ''))
  28. prices = re.findall(r'\$(\d+\.\d+)', text)
  29. if prices:
  30. result += f'prices: ${prices[0]}\n'
  31. volumes = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  32. if volumes:
  33. result += f'24-hour trading volume: ${volumes[1]}\n'
  34. change = re.findall(r'(up|down) (\d+\.\d+)%', text)
  35. if change:
  36. c = ' '.join(change[0])
  37. result += f'change: {c}%\n'
  38. live_market_cap = re.findall(r'\$(\d{1,3}(?:,\d{3})*(?:\.\d+)?)', text)
  39. if live_market_cap:
  40. if len(live_market_cap) == 3:
  41. result += f'live market cap: ${live_market_cap[2]}\n'
  42. else:
  43. pass
  44. max_circulating_supply = re.findall(r'(\d{1,3}(?:,\d{3})*)', text)
  45. if max_circulating_supply:
  46. result += f'max circulating supply: {max_circulating_supply[-1]}\n'
  47. result += '\n'
  48. print(result)
  49. except httpx.RequestError as e:
  50. # 打印错误信息
  51. print(f"请求错误: {e}")
  52. except Exception as e:
  53. # 打印其他错误信息
  54. print(f"发生错误: {e}")