upload
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, r'C:\Users\mjz\Desktop\python\9月\歪马送酒\AI代码')
|
||||
import newsetting as ten
|
||||
|
||||
|
||||
# 任务库
|
||||
tent = ten.All_Setting(host='192.168.5.5', port=3306, db='project_waima')
|
||||
|
||||
COUPON_TABLE = 'waima_coupon_detail' # 券商品页, 一行一页
|
||||
SEARCH_TABLE = 'waima_keyword_search' # 搜索页, 一行一页
|
||||
SUM_TABLE = 'waima_page_summary' # 页级汇总表, 先执行 汇总表.sql
|
||||
WATER_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '汇总水位.json') # 增量水位
|
||||
BATCH_LIMIT = 2000 # 每批读多少行, 防止一次全读进内存
|
||||
CLEAR_BEFORE = False # True=先把汇总表清空再补(清过原始表重跑时打开)
|
||||
|
||||
|
||||
def get_water():
|
||||
"""读上次补到哪个 id"""
|
||||
if not os.path.exists(WATER_FILE):
|
||||
return {'coupon': 0, 'search': 0}
|
||||
return json.load(open(WATER_FILE, encoding='utf-8'))
|
||||
|
||||
|
||||
def save_water(water):
|
||||
json.dump(water, open(WATER_FILE, 'w', encoding='utf-8'))
|
||||
|
||||
|
||||
def get_max_id(table):
|
||||
rows = tent.get_shoppingmalls(f'select max(id) from {table}')
|
||||
return rows[0][0] or 0
|
||||
|
||||
|
||||
def fix_water(source, table, last_id):
|
||||
"""清表重跑后 id 会变小, 水位跟着归零, 否则新页永远补不进来"""
|
||||
max_id = get_max_id(table)
|
||||
if max_id < last_id:
|
||||
print(f'{source}: 原始表 max(id)={max_id} 小于水位 {last_id}, 判定清表重跑, 水位归零')
|
||||
return 0
|
||||
return last_id
|
||||
|
||||
|
||||
def clean_coupon_page(rawdata):
|
||||
"""券商品页: 本页件数 / hasMore / 命中总数(券为空) / spuId串"""
|
||||
data = json.loads(rawdata)['data']
|
||||
# 商品现在在 cards[].data 里(products 是空的), 老数据的页才在 products
|
||||
items = [card['data'] for card in data['cards']]
|
||||
if not items:
|
||||
items = data['products']
|
||||
return len(items), 1 if data['hasMore'] else 0, None, ','.join(str(item['spuId']) for item in items)
|
||||
|
||||
|
||||
def clean_search_page(rawdata):
|
||||
"""搜索页: 本页件数 / hasNextPage / 命中总数 / spuId串"""
|
||||
data = json.loads(rawdata)['data']
|
||||
cards = data['cards']
|
||||
return len(cards), 1 if data['hasNextPage'] else 0, data['totalNum'], ','.join(str(card['data']['spuId']) for card in cards)
|
||||
|
||||
|
||||
def build_summary_rows(source, biz_col, table, clean_page, last_id):
|
||||
"""分批读原始页, 清洗成汇总行; 返回 (汇总行, 处理到的最大id)"""
|
||||
rows = []
|
||||
while True:
|
||||
sql = f'''
|
||||
select id, batch, store_id, store_name, {biz_col}, page, rawdata from {table} where id > {last_id} order by id limit {BATCH_LIMIT}
|
||||
'''
|
||||
raw_rows = tent.get_shoppingmalls(sql)
|
||||
if not raw_rows:
|
||||
break
|
||||
for row_id, batch, store_id, store_name, biz_id, page, rawdata in raw_rows:
|
||||
product_count, has_more, total_num, spu_ids = clean_page(rawdata)
|
||||
rows.append((source, batch, store_id, store_name, biz_id, int(page), product_count, has_more, total_num, spu_ids))
|
||||
last_id = row_id
|
||||
print(f' {source} 读到 id={raw_rows[0][0]}~{last_id}, 累计 {len(rows)} 行')
|
||||
return rows, last_id
|
||||
|
||||
|
||||
def insert_summary(rows):
|
||||
"""汇总行入库; 主键冲突就刷新数字(采集脚本可能已经写过同一页)"""
|
||||
sql = f'''
|
||||
insert into {SUM_TABLE} (source, batch, store_id, store_name, biz_id, page, product_count, has_more, total_num, spu_ids) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||
on duplicate key update product_count=values(product_count),has_more=values(has_more),total_num=values(total_num),spu_ids=values(spu_ids)
|
||||
'''
|
||||
tent.write_data(sql, rows)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if CLEAR_BEFORE:
|
||||
print('CLEAR_BEFORE=True, 先清空汇总表 %s' % SUM_TABLE)
|
||||
tent.update_data(f'delete from {SUM_TABLE}')
|
||||
|
||||
water = get_water()
|
||||
print('当前水位: %s' % water)
|
||||
|
||||
coupon_last = fix_water('coupon', COUPON_TABLE, water['coupon'])
|
||||
coupon_rows, water['coupon'] = build_summary_rows('coupon', 'coupon_id', COUPON_TABLE, clean_coupon_page, coupon_last)
|
||||
print('券商品页 清洗 %d 行' % len(coupon_rows))
|
||||
|
||||
search_last = fix_water('search', SEARCH_TABLE, water['search'])
|
||||
search_rows, water['search'] = build_summary_rows('search', 'keyword', SEARCH_TABLE, clean_search_page, search_last)
|
||||
print('搜索页 清洗 %d 行' % len(search_rows))
|
||||
|
||||
all_rows = coupon_rows + search_rows
|
||||
if all_rows:
|
||||
insert_summary(all_rows)
|
||||
print('汇总表入库 %d 行' % len(all_rows))
|
||||
else:
|
||||
print('没有新页要补')
|
||||
|
||||
save_water(water)
|
||||
print('水位已存: %s' % water)
|
||||
Reference in New Issue
Block a user