PUGE преди 2 месеца
родител
ревизия
80c45f2bd3
променени са 2 файла, в които са добавени 31 реда и са изтрити 19 реда
  1. 31 19
      app.py
  2. BIN
      buff_prices.db

+ 31 - 19
app.py

@@ -1,5 +1,4 @@
 import time
-
 import requests
 import sqlite3
 from datetime import datetime
@@ -8,7 +7,7 @@ from datetime import datetime
 def get_buff_data(page_num):
     """从Buff API获取商品数据"""
     url = "https://buff.163.com/api/market/goods"
-    querystring = {"game": "csgo","page_size": 80, "page_num": page_num, "tab": "selling"}
+    querystring = {"game": "csgo"}
     headers = {
         "accept": "application/json, text/javascript, */*; q=0.01",
         "accept-language": "zh-CN,zh;q=0.9",
@@ -22,19 +21,26 @@ def get_buff_data(page_num):
         "sec-fetch-site": "same-origin",
         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
         "x-requested-with": "XMLHttpRequest",
-        "Cookie": "NTES_YD_SESS=9Ko5g05jJPzL0nDaqrG7L0B9n7AiGVSy0Q4Rh7lAlP3Ll.DwlvG8Q0qiKkN_BGxhLcofy9IT_mEPLnlsRxJMcT2j2ZHJdIidq2YgzyU9uC._XmVDURPonklYKRXNyoZ68AzWP5XQjf7Bz1ouZKiPQ5hfZoAcTClcSb0NUg2ijt4Ce5EYAMxsLEvFAnPKKgSjKIdOtZSpSJpOcBu11Bh9Ccum0uxDMGYj9; S_INFO=1774149977|0|0&60##|18511117532; P_INFO=18511117532|1774149977|1|netease_buff|00&99|null&null&null#xiz&540100#10#0|&0||18511117532; remember_me=U1089937100|UUBCBtmtPIUdtQZz9YmlUBjQ28AfdV8m; session=1-5oyvnvmyB29IGHS6qhrIkkZEjN-Jy2mGZ6NU34Eh3UPf2017034644; csrf_token=IjY0Mzg5OTRhYmY5MTFmNGU2MzBjZDI0ZjAwZWQxYzQ1YTdmYjEyZmUi.ab9hYA.awg5FDUiey0d1h9yU_kdomsG6lo",
         "Accept-Encoding": "gzip, deflate, br",
         "Connection": "keep-alive"
     }
     proxies={
-        "http": "http://USER660348-zone-custom-region-HK:e8b389:hk.rotgb.711proxy.com:10000",
-        "https": "http://USER660348-zone-custom-region-HK:e8b389:hk.rotgb.711proxy.com:10000",
+        "http": "socks5://USER660348-zone-custom-region-HK:e8b389@hk.rotgb.711proxy.com:10000",
+        "https": "socks5://USER660348-zone-custom-region-HK:e8b389@hk.rotgb.711proxy.com:10000",
     }
-    response = requests.get(url, headers=headers, params=querystring, proxies = proxies)
-    return response.json()
+    try:
+        response = requests.get(url, headers=headers, params=querystring, proxies = proxies)
+        return response.json()
+    except Exception as e:
+        print(f"请求出错: {e}")
+        return None
 
 def save_to_database(data):
     """保存数据到数据库"""
+    if data is None:
+        print("数据为空,跳过保存")
+        return
+    
     conn = sqlite3.connect('buff_prices.db')
     cursor = conn.cursor()
     current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
@@ -125,19 +131,25 @@ def save_to_database(data):
 
 def main():
     ind = 0
-    # while ind < 400:
-    ind = ind + 1
-    # 获取Buff数据
-    print("正在获取Buff数据,页码:" + str(ind))
-    response_data = get_buff_data(ind)
+    while True:
+        try:
+            # ind = ind + 1
+            # 获取Buff数据
+            # print("正在获取Buff数据,页码:" + str(ind))
+            response_data = get_buff_data(ind)
+                
+            if response_data and response_data.get('code') == 'OK':
+                # 保存到数据库
+                save_to_database(response_data)
+            else:
+                if response_data:
+                    print(f"API返回错误: {response_data.get('msg')}")
+                else:
+                    print("获取数据失败")
+        except Exception as e:
+            print(f"主循环出错: {e}")
         
-    if response_data.get('code') == 'OK':
-        # 保存到数据库
-        save_to_database(response_data)
-    else:
-        print(f"API返回错误: {response_data.get('msg')}")
-
-        # time.sleep(5)
+        time.sleep(30)
         
 if __name__ == "__main__":
     main()

BIN
buff_prices.db