jack 1 gadu atpakaļ
vecāks
revīzija
093ab7e473
3 mainītis faili ar 81 papildinājumiem un 4 dzēšanām
  1. 3 2
      message/message_coinmarketcap.py
  2. 72 0
      test/test_sui.py
  3. 6 2
      utils/utils_check_base.py

+ 3 - 2
message/message_coinmarketcap.py

@@ -53,11 +53,12 @@ def get_coinmarketcap_coin_price():
         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.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)
 
         prices = re.findall(r'\$(\d+\.\d+)', text)
         if prices:

+ 72 - 0
test/test_sui.py

@@ -0,0 +1,72 @@
+# -*- 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/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}")

+ 6 - 2
utils/utils_check_base.py

@@ -15,7 +15,7 @@ from utils.utils_logs_handle import LogsHandle
 
 
 class CryptoCrawler:
-    def __init__(self, url_list, selectors, check_difference=False, headless=True):
+    def __init__(self, url_list, selectors, check_difference=False, headless=True, proxy=False):
         self.url_list = url_list
         self.selectors = selectors
         self.check_difference = check_difference  # 用于检测数据是否发生变化 (开关)
@@ -24,10 +24,14 @@ class CryptoCrawler:
         self.db = 'CHECK'
         self.collection = 'check'
         self.headless = headless
+        self.proxy = proxy
 
     def main(self):
         with sync_playwright() as playwright:
-            browser = playwright.webkit.launch(headless=self.headless)
+            if self.proxy:
+                browser = playwright.webkit.launch(headless=self.headless, proxy={'server': '127.0.0.1:7890'})
+            else:
+                browser = playwright.webkit.launch(headless=self.headless)
             context = browser.new_context(viewport={'width': 1920, 'height': 1080})
             page = context.new_page()