import json from jsonpath_ng import parse import pymysql import requests from newsetting import All_Setting import re from decimal import Decimal mapping_dict = { "非及时达标签 1": ["预售", "厂家直发", "云超特卖", "xxH 发货"], "非及时达标签 2": ["预约", "商家直发", "云卖场", "xxH 内发货"], "非及时达标签 3": ["明日达", "厂商直发", "云超商城", "xx 小时内发快递"], "非及时达标签 4": ["明日送达", "快递发货", "云仓特卖", "XX 陆续发货"], "非及时达标签 5": ["次日达", "产地寻鲜", "甄选商城", "xx 月 xx 日发货"], "非及时达标签 6": ["多日达", "地产量贩", "社区团购", "xx.xx 送达"], "非及时达标签 7": ["即将开售", "跨境购", "xx 月 xx 日送达"], } tag_mapping = { 312: "冷藏", 319: "冷冻", 378: "明日达", 274: "严选", 320: "新品", 399: "地标", 430: "元宵节", 318: "冷鲜", 377: "无抗", 297: "秒杀价", 365: "新鲜快手菜", 198: "辣度2星", 199: "辣度1星", 163: "省心价", 263: "省心价", 379: "中国有机产品", 293: "日日鲜", 359: "黑金系列", 366: "手工鲜切", 148: "预定" } def find_all_values_by_key(data, target_key): results = [] if isinstance(data, dict): for key, value in data.items(): if key == target_key: results.append(value) # 如果值是字典或列表,则递归查找 if isinstance(value, (dict, list)): results.extend(find_all_values_by_key(value, target_key)) elif isinstance(data, list): for item in data: results.extend(find_all_values_by_key(item, target_key)) return results def ocr_1(img_url): import requests from io import BytesIO url = "http://192.168.5.2:9002/infer" # img_url = 'https://p0.meituan.net/mallimages/04e59006293302d0808a5e9bff01d17a79341.png' img_response = requests.get(img_url, timeout=10) img_response.raise_for_status() img_file = BytesIO(img_response.content) files = {"file": ("your.jpg", img_file, "image/jpeg")} r = requests.post(url, files=files, timeout=60) return r.json()['full_text'] def clean_price(price_str): if not price_str: return 0.0 clean_str = re.sub(r'[^\d.]', '', price_str) try: return float(clean_str) except ValueError: return 0.0 tent2 = All_Setting(host='183.6.7.91', port=33016, db='tk_test') shop_id = 10000900 sql3 = f""" select * FROM category_detail_log where shop_id= '{shop_id}' """ da123 = tent2.get_shoppingmalls(sql3) for da in da123: sql3 = f""" select * FROM category_detail where shop_id= '{shop_id}' and one_id='{str(da[3])}' and two_id='{str(da[4])}' limit 1 """ da1 = tent2.get_shoppingmalls(sql3) try:data = json.loads(da[5])["data"]["contents"][0]["subModule"]["contents"] except: continue all_clean_data = [] print(f'当前1分类{da1[0][3]},2分类{da1[0][5]}') for i in data: all_image_urls = find_all_values_by_key(i, 'image') ocr_results = [] for item in all_image_urls: img_url = None try: if isinstance(item, dict) and 'url' in item: img_url = item['url'] elif isinstance(item, str) and item.startswith('http'): img_url = item else: print(f"跳过无效数据: {item}") continue if img_url and img_url.startswith('http'): ocr_results.append(img_url) except Exception as e: continue all_clean_data.append({ 'one_name': da1[0][3], 'one_id': da[3], 'two_name': da1[0][5], 'two_id': da[4], 'shop_name': i.get("lxInfo", {}).get("content_name", ""), 'skuId': i.get("logicContents", {}).get("skuId", ""), 'label': i.get("lxInfo", {}).get("label", ""), 'sale_type': i.get("lxInfo", {}).get("sale_type", ""), 'showContents': ocr_results, 'sale_price': i.get("lxInfo", {}).get("sale_price", 0.0), 'shop_buyUnit': i.get("logicContents", {}).get("buyUnit", ""), 'origin_price': i.get("lxInfo", {}).get("origin_price", 0.0), 'Next_day_delivery': 0, }) sql = """ INSERT INTO product_data (shop_name, sku_id, label, sale_type, show_contents, sale_price, buy_unit, origin_price, one_name, one_id, two_name, two_id, next_day_delivery) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ for item in all_clean_data: print(item) values = ( item['shop_name'], item['skuId'], item['label'], item['sale_type'], str(item['showContents']), clean_price(item['sale_price']), item['shop_buyUnit'], clean_price(item['origin_price']), item['one_name'], item['one_id'], item['two_name'], item['two_id'], item['Next_day_delivery'] ) try: tent2.write_data_one(sql, values) except Exception as e: print(f"插入数据时出错: {e}")