# -*- coding: utf-8 -*- # Author : Charley # Python : 3.10.8 # Date : 2025/2/17 16:22 import inspect import requests import user_agent from loguru import logger from tenacity import retry, stop_after_attempt, wait_fixed from mysq_pool import MySQLConnectionPool logger.remove() logger.add("./logs/{time:YYYYMMDD}.log", encoding='utf-8', rotation="00:00", format="[{time:YYYY-MM-DD HH:mm:ss.SSS}] {level} {message}", level="DEBUG", retention="7 day") def after_log(retry_state): """ retry 回调 :param retry_state: RetryCallState 对象 """ # 检查 args 是否存在且不为空 if retry_state.args and len(retry_state.args) > 0: log = retry_state.args[0] # 获取传入的 logger else: log = logger # 使用全局 logger if retry_state.outcome.failed: log.warning( f"Function '{retry_state.fn.__name__}', Attempt {retry_state.attempt_number} Times") else: log.info(f"Function '{retry_state.fn.__name__}', Attempt {retry_state.attempt_number} succeeded") @retry(stop=stop_after_attempt(5), wait=wait_fixed(1), after=after_log) def get_proxys(log): """ 获取代理 :return: 代理 """ tunnel = "x371.kdltps.com:15818" kdl_username = "t13753103189895" kdl_password = "o0yefv6z" try: proxies = { "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": kdl_username, "pwd": kdl_password, "proxy": tunnel}, "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": kdl_username, "pwd": kdl_password, "proxy": tunnel} } return proxies except Exception as e: log.error(f"Error getting proxy: {e}") raise e @retry(stop=stop_after_attempt(5), wait=wait_fixed(1), after=after_log) def get_request_one_page(log, rating_no) -> dict: headers = { "accept": "*/*", "accept-language": "en,zh-CN;q=0.9,zh;q=0.8", "content-type": "application/json;charset=UTF-8", "origin": "https://www.zhongjianjiantong.com", "priority": "u=1, i", "referer": "https://www.zhongjianjiantong.com/web/index.html", "sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"", "sec-ch-ua-mobile": "?1", "sec-ch-ua-platform": "\"Android\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-origin", "user-agent": user_agent.generate_user_agent() } url = "https://www.zhongjianjiantong.com/Api/OrderRatingGoods/detail" data = { "rating_no": rating_no } try: with requests.Session() as session: response = session.post(url, headers=headers, json=data, proxies=get_proxys(log), timeout=5) # print(response.text) response.raise_for_status() return response.json() except Exception as e: log.warning(f"{inspect.currentframe().f_code.co_name} error: {e}") return {} def parse_data(resp_json, sql_pool): card_id = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('id') order_no = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('order_no') tag_no = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('tag_no') # 标签号/查询的号码 images = resp_json.get('data', {}).get('obj_order_rating_goods', []).get('images') card_create_time = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('create_time') card_update_time = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('update_time') score = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('score') # 中检评分 corners = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('corners') # 四角 eoges = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('eoges') # 边缘 surface = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('surface') # 表面 centering = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('centering') # 居中 colour = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('colour') # 颜色 repair = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('repair') # 修复 rating_no = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('rating_no') # 证书编号 obj_brand_title = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_brand', {}).get( 'title') # 商品品牌 obj_detail_spxl = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get( 'spxl') # 商品系列 obj_detail_spmc = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get( 'spmc') # 商品名称 obj_detail_fxnf = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get( 'fxnf') # 发行年份 obj_detail_yy = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get('yy') # 语言 obj_detail_spbh = resp_json.get('data', {}).get('obj_order_rating_goods', {}).get('obj_detail', {}).get( 'spbh') # 商品编号 info = ( card_id, order_no, tag_no, images, card_create_time, card_update_time, score, corners, eoges, surface, centering, colour, repair, rating_no, obj_brand_title, obj_detail_spxl, obj_detail_spmc, obj_detail_fxnf, obj_detail_yy, obj_detail_spbh) sql = """ INSERT INTO zhongjian_record (card_id, order_no, tag_no, images, card_create_time, card_update_time, score, corners, eoges, surface, centering, colour, repair, rating_no, obj_brand_title, obj_detail_spxl, obj_detail_spmc, obj_detail_fxnf, obj_detail_yy, obj_detail_spbh) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ sql_pool.insert_one(sql, info) def control_rating_no(log, sql_pool): # rating_no_ = '519531553' for i in range(871527, 1000000): rating_no_ = f'519{i:06}' try: log.info(f"{rating_no_} is start ......................................") resp_json = get_request_one_page(log, rating_no_) if resp_json and resp_json.get('code') == 200: # print(resp_json) parse_data(resp_json, sql_pool) elif resp_json and resp_json.get('code') == 400: log.warning(f"{rating_no_} is not exist ......................................") else: log.warning(f"other warning, please check ......................................") except Exception as e: log.warning(f"{inspect.currentframe().f_code.co_name} error: {e}") continue @retry(stop=stop_after_attempt(50), wait=wait_fixed(600), after=after_log) def zhongjian_main(log): """ 主函数 :param log: """ log.info( f'开始运行 {inspect.currentframe().f_code.co_name} 爬虫任务....................................................') # 配置 MySQL 连接池 sql_pool = MySQLConnectionPool(log=log) if not sql_pool: log.error("MySQL数据库连接失败") raise Exception("MySQL数据库连接失败") try: control_rating_no(log, sql_pool) except Exception as e: log.error(f'{inspect.currentframe().f_code.co_name} error: {e}') finally: log.info(f'爬虫程序 {inspect.currentframe().f_code.co_name} 运行结束,等待下一轮的采集任务............') if __name__ == '__main__': zhongjian_main(logger)