Files
F6--/张阳脚本/竞品系统数据导出/会员卡导出.py
T
2026-04-18 09:22:23 +08:00

89 lines
3.6 KiB
Python

import requests
import pandas as pd
cookies = {
'JSESSIONID': 'FA68674FDDA302C51E2775091B995EEA',
'td_cookie': '3009435466',
}
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Referer': 'http://www.idsz.xin:7070/report_member_verifi_list?detailtype=1&key=totalCount&datafrom=2026-01-01&datato=2026-04-13&sshopId=&type=1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0',
'X-Requested-With': 'XMLHttpRequest',
}
def get_data(page=0, page_size=50):
url = f'http://www.idsz.xin:7070/posapi_invoke?apiname=kpi_memberVerifiAndSurplusQuery&detailtype=1&startTime=2026-01-01&endTime=2026-04-13&key=totalCount&sshopId=&timesCardId=&option=&page={page}&pageSize={page_size}'
response = requests.get(url=url, headers=headers, cookies=cookies)
response.raise_for_status()
return response.json()
print("正在请求会员卡数据...")
try:
# 先获取第一页数据,获取总数
first_page = get_data(page=0)
total = first_page.get('total', 0)
page_size = 50
total_pages = (total + page_size - 1) // page_size
print(f"数据总条数: {total}")
print(f"每页条数: {page_size}")
print(f"总页数: {total_pages}")
TCK = []
# 获取第一页数据
if 'rows' in first_page and len(first_page['rows']) > 0:
for row in first_page['rows']:
TCK1 = {
'车牌号': row.get('carNo', ''),
'卡名称': row.get('cardType', ''),
'到期时间': row.get('endTime', ''),
'发动机号': row.get('engineNumber', ''),
'剩余明细': row.get('goodsName', ''),
'剩余次数': row.get('qty', ''),
'手机号': row.get('mobilePhone', ''),
'客户姓名': row.get('name', ''),
'备注': row.get('remark', ''),
'Vin码': row.get('vin', '')
}
TCK.append(TCK1)
print(f"已获取第1页数据,累计 {len(TCK)}")
# 获取剩余页数据
for page in range(1, total_pages):
try:
data = get_data(page=page)
if 'rows' in data and len(data['rows']) > 0:
for row in data['rows']:
TCK1 = {
'车牌号': row.get('carNo', ''),
'卡名称': row.get('cardType', ''),
'到期时间': row.get('endTime', ''),
'发动机号': row.get('engineNumber', ''),
'剩余明细': row.get('goodsName', ''),
'剩余次数': row.get('qty', ''),
'手机号': row.get('mobilePhone', ''),
'客户姓名': row.get('name', ''),
'备注': row.get('remark', ''),
'Vin码': row.get('vin', '')
}
TCK.append(TCK1)
print(f"已获取第{page+1}页数据,累计 {len(TCK)}")
except Exception as e:
print(f"获取第{page+1}页数据失败: {e}")
continue
# 导出数据
df = pd.DataFrame(TCK)
output_path = '会员卡.xlsx'
df.to_excel(output_path, index=False)
print(f"\n成功导出 {len(TCK)} 条会员卡数据到 {output_path}")
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
except ValueError as e:
print(f"JSON解析失败: {e}")