import json import os import random import time from openpyxl import load_workbook import requests from openpyxl import Workbook url = 'https://www.picchealth.com/eportal/ui?moduleId=9bd0917443454517a791cc11fdaddfae&struts.portlet.action=/portlet/cashValueFront!queryResult.action' # c_xb_lst = ['男性', '女性'] # c_tbnl_list = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, # 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, # 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55] # c_bdnd_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, # 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, # 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, # 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, # 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, # 106] # c_jfqj_list = ['1年交', '3年交', '5年交', '10年交'] c_xb_lst = ['男性'] c_tbnl_list = [2] c_bdnd_list = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106] c_jfqj_list = ['1年交', '3年交', '5年交', '10年交'] num = 886 save_path = '人保健康欣安福互联网重大疾病保险.xlsx' if os.path.exists(save_path) == True: print(f'{save_path}已存在,追加数据') # 加载现有的Excel工作簿 wb = load_workbook('人保健康欣安福互联网重大疾病保险.xlsx') # 选择要追加数据的工作表 ws = wb.active else: print(f'{save_path}不存在,新建数据') # 创建一个新的工作簿 wb = Workbook() # 选择默认的工作表 ws = wb.active sum_len = len(c_xb_lst) * len(c_tbnl_list) * len(c_bdnd_list) * len(c_jfqj_list) for c_xb in c_xb_lst: time.sleep(random.randint(1, 2)) for c_tbnl in c_tbnl_list: time.sleep(random.randint(1, 2)) for c_bdnd in c_bdnd_list: time.sleep(random.randint(1, 2)) for c_jfqj in c_jfqj_list: time.sleep(random.randint(3, 5)) data = { 'c_type': '20240419134723_372011', 'c_xb': c_xb, 'c_tbnl': c_tbnl, 'c_bdnd': c_bdnd, 'c_jfqj': c_jfqj, 'c_bxnx': '保终身' } headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0' } html = requests.post(url=url, headers=headers, data=data).content.decode() price = json.loads(html)['data'] if price == '-': price = 0 print('未查询到该产品的现金价值相关信息') else: price = float(price) print(f'c_xb:{c_xb}') print(f'c_tbnl:{c_tbnl}') print(f'c_bdnd:{c_bdnd}') print(f'c_jfqj:{c_jfqj}') print(price) print(f'共有{sum_len}条数据') print(f'存入第{num}条数据') print('\n') # 数据可以直接分配到单元格中 ws['A1'] = "性别" ws['B1'] = "投保年龄" ws['C1'] = "投保年度" ws['D1'] = "交费期间" ws['E1'] = "保险期间" ws['F1'] = "现金价值" ws[f'A{num}'] = c_xb ws[f'B{num}'] = c_tbnl ws[f'C{num}'] = c_bdnd ws[f'D{num}'] = c_jfqj ws[f'E{num}'] = '保终身' ws[f'F{num}'] = price # 保存工作簿到文件 wb.save(save_path) print('\n') num += 1
标签:文件,jfqj,python,写入,list,xb,ws,print,tbnl From: https://www.cnblogs.com/wolvies/p/18179784