shensong00 3 years ago
parent
commit
5973c5cc19
1 changed files with 41 additions and 31 deletions
  1. 41 31
      douyin_hourly_ranklist_commerce_scraper.py

+ 41 - 31
douyin_hourly_ranklist_commerce_scraper.py

@@ -231,42 +231,52 @@ def get_commerce_rank_list_data(room_id, sec_anchor_id, anchor_id, result):
231 231
 def scrape(room_id,sec_anchor_id,anchor_id):
232 232
     rds_list = RdsDouyinHourlyRankList()
233 233
 
234
-    key = 'DOUYIN_SCRAPE_DID_IID_TTREQ_1221'
235
-    rdid = DbRedis.connect().rpop(key)
236
-    if rdid:
237
-        result = rdid.split('@@@')
238
-    else:
239
-        result = []
240
-        return None
234
+    start_time = int(time.time())
241 235
 
242
-    DbRedis.connect().lpush(key, rdid)
236
+    while True:
243 237
 
244
-    try:
245
-        # 带货小时榜
246
-        commerce_response_json = get_commerce_rank_list_data(room_id=room_id, sec_anchor_id=sec_anchor_id, anchor_id=anchor_id, result=result)
247
-        
248
-        if commerce_response_json is None:
238
+        current_time = int(time.time())
239
+        if current_time - start_time >= 9*60:
249 240
             print(
250
-                time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
251
-                + ' 带货小时榜数据获取失败!响应数据为空!' 
252
-            )
241
+                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
242
+                    + ' 带货小时榜数据获取失败!响应数据为空!' 
243
+                )
253 244
             sys.exit(0)
245
+
246
+        key = 'DOUYIN_SCRAPE_DID_IID_TTREQ_1221'
247
+        rdid = DbRedis.connect().rpop(key)
248
+        if rdid:
249
+            result = rdid.split('@@@')
254 250
         else:
255
-            data = json.dumps({
256
-                "data": commerce_response_json.get('data'),
257
-                "extra": {}
258
-            })
259
-            rds_list.push_commerce_data_list(data)
260
-
261
-    except Exception as e:
262
-        print(
263
-            time.strftime("%H:%M:%S", time.localtime()) 
264
-            + ' ' 
265
-            + '数据异常:' 
266
-            + str(e)
267
-        )
268
-
269
-    sys.exit(0)
251
+            result = []
252
+            return None
253
+
254
+        DbRedis.connect().lpush(key, rdid)
255
+
256
+        try:
257
+            # 带货小时榜
258
+            commerce_response_json = get_commerce_rank_list_data(room_id=room_id, sec_anchor_id=sec_anchor_id, anchor_id=anchor_id, result=result)
259
+            
260
+            if commerce_response_json is None:
261
+                print(
262
+                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
263
+                    + ' 带货小时榜数据获取失败!响应数据为空!' 
264
+                )
265
+            else:
266
+                data = json.dumps({
267
+                    "data": commerce_response_json.get('data'),
268
+                    "extra": {}
269
+                })
270
+                rds_list.push_commerce_data_list(data)
271
+                sys.exit(0)
272
+
273
+        except Exception as e:
274
+            print(
275
+                time.strftime("%H:%M:%S", time.localtime()) 
276
+                + ' ' 
277
+                + '数据异常:' 
278
+                + str(e)
279
+            )
270 280
 
271 281
 if __name__ == "__main__":
272 282
     print("主方法开始执行")