[Python] 纯文本查看 复制代码
#-*-coding=utf-8-*-#
''' 默认没有问题的填法 '''
import requests,json
from requests_toolbelt.multipart.encoder import MultipartEncoder
from PIL import Image
import urllib
import re
import sys
import xdrlib,sys
import xlrd
import xlwt
import os
from xlutils.copy import copy
import html5lib
import time
import urllib3
import easygui
import random
def check(IDcard): # 按# 传入身份证号码,,返回正确与否以及信息,数组形式。
if len(IDcard) != 18:
var = '号码:' + IDcard + "__身份证号码位数不对!__错误的身份证号码.__请重新输入!__"
valid ='False'
else:
IDcard_add = IDcard[0:6] # 身份证前6位,对应归属地
IDcard_birth = IDcard[6:14] # 身份证中间8位,对应出生日期
IDcard_sex = IDcard[14:17] # 身份证15,,16,17位,对应性别
area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁",
"22": "吉林", "23": "黑龙江", "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽",
"35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北","43": "湖南",
"44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州",
"53": "云南", "54": "西藏", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏",
"65": "新疆", "71": "台湾", "81": "香港", "82": "澳门", "91": "国外"}
# 地区校验
if IDcard[0:2] not in area.keys():
var = '号码:' + IDcard + '__身份证地区非法!__错误的身份证号码__'
valid = 'False'
else:
year = IDcard_birth[0:4] # 出生年份
month = IDcard_birth[4:6] # 出生月份
day = IDcard_birth[6:8] # 出生日
# 出生日期的合法性检查
# 闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]
# |[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
# 平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]
# |[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
if (int(year) % 4 == 0 or (int(year) % 100 == 0 and int(year) % 4 == 0)):
ereg = re.compile('[1-9][0-9]{5}((19[0-9]{2})|(20[0-1][0-8]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]'
'|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))'
'[0-9]{3}[0-9Xx]$') # //闰年出生日期的合法性正则表达式
else:
ereg = re.compile('[1-9][0-9]{5}((19[0-9]{2})|(20[0-1][0-8]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]'
'|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8])'
')[0-9]{3}[0-9Xx]$') # //平年出生日期的合法性正则表达式
# //测试出生日期的合法性
if (re.match(ereg, IDcard)):
IDcard_check = IDcard[17] # 身份证最后一位
W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 逐位权数
IDcard_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] # 身份证检验位
IDcard_sum = 0 # 定义累加和
for i in range(0, 17):
IDcard_sum = IDcard_sum + int(IDcard[i]) * W[i] # 逐位加权求和
IDcard_Check = IDcard_sum % 11 # 取余数
if IDcard_check != IDcard_CHECK[IDcard_Check]: # 和检验位对比
var = '号码:' + IDcard + '__身份证号码校验错误!__错误的身份证号码__'
valid = 'False'
else:
if int(IDcard_sex) % 2 == 0:
var = "正确的身份证号码! __" + '号码:' + IDcard + '__地区:' + area[IDcard[0: 2]] + \
'__生日:' + year + '年' + month + '月' + day + '日' + "__ 性别:女__"
valid = 'True'
else:
var = "正确的身份证号码! __" + '号码:' + IDcard + '__地区:' + area[IDcard[0: 2]] + \
"__生日: " + year + '年' + month + '月' + day + '日' + "__性别:男 __"
valid = 'True'
else:
var = '号码:' + IDcard + '__身份证号码出生日期超出范围或含有非法字符!__错误的身份证号码__'
valid = 'False'
#print(var)
return valid, var
urllib3.disable_warnings()
# #变化部分
token = "" #自行抓微信包获取
Referer=""#自行抓微信包获取
appId=""#自行抓微信包获取
url = 'https://hsjc6.zdkqyy.com/ncov-nat-api/nat/sample/save/natSelfReportPerson'
urlqr="https://hsjc6.zdkqyy.com/ncov-nat-api/nat/sample/getNatSelfReportPersonQrcode"
headers = {
'Host': 'hsjc6.zdkqyy.com',
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'token': token,#变化部分
'version': 'V20220111',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat',
'Referer': Referer,#变化部分
}
path = easygui.fileopenbox()
#print(path)
jieguoname = path.replace( ".xlsx", "(处理结果).xlsx" )
data = xlrd.open_workbook(path)
wb = copy( data ) # 写入表格
ws = wb.get_sheet( 0 ) # 写入表格
for x in range( 0, 1 ):
table = data.sheets()[x] # 0代表第一个表单
nrows = table.nrows # 行
ncols = table.ncols # 列数
#print(nrows)
#print(ncols)
for i in range( 1, nrows ): # nrows
personName = table.cell( i, 1 ).value # 姓名
identityNumber = table.cell( i, 2 ).value# 身份证号码
try:
identityNumber=int(identityNumber)
except:
pass
return_list = check(str(identityNumber))
if return_list[0] == "False":
#print( '错误' )
#print( return_list[1] )
ws.write(i,9,return_list[1])
i+=1
else:
#print( return_list[1] )
try:
phoneNumber = int(table.cell( i, 3 ).value)# 手机号码
except:
phoneNumber = 5234088
gender1 = table.cell( i, 4 ).value # 性别
if gender1=="男":
gender=1
else:
gender=2
age = int(table.cell( i, 5 ).value) # 年龄
print(i,personName,identityNumber,phoneNumber)
postdata={
"data":{"identityValid":"1",
"identityType":"1",
"nationality":"156",
"personSource":"4",
"personIdentity":"99",
"insuranceType":"1",
"overseasPersonnel":"0",
"personDataSource":"6",
"sampleDate":"2022-01-12 13:48:00",
"sampleList":[{"sampleBarcode":"","sampleType":"2","detectionItem":"1","paymentType":"3"}],
"personName":personName,
"phoneNumber":phoneNumber,
"identityNumber":identityNumber,
"gender":gender,
"age":age}}
r = requests.post(url, headers=headers,data=json.dumps(postdata),verify=False)
time.sleep(random.uniform(0.5,1.5))
print(r.url,r.status_code,r.text)#r.text
r_data=json.loads(r.text)
print(r_data["statusCode"])
if r_data["statusCode"]=="000000":
print(r_data["resultData"]["personName"],"上传成功")
ws.write(i,9,r_data["resultData"]["id"])
print(r_data["resultData"]["id"])
qrpostdata={
"id":r_data["resultData"]["id"],
"appId":appId}#appID 需要根据实际情况更换。
qr_r = requests.post(urlqr, headers=headers,data=json.dumps(qrpostdata),verify=False)
qr_data=json.loads(qr_r.text)
print(qr_data)
ws.write(i,10,qr_data["resultData"]["qrCode"])
else:
("上传失败")
ws.write(i,9,"上传失败")
wb.save(jieguoname)