python批量生成身份证号到Excel的两种方法实例


Posted in Python onJanuary 14, 2021

身份证号码的编排规则

前1、2位数字表示:所在省份的代码;

第3、4位数字表示:所在城市的代码;

第5、6位数字表示:所在区县的代码;

第7~14位数字表示:出生年、月、日;

第15、16位数字表示:所在地的派出所的代码;

第17位数字表示性别:奇数表示男性,偶数表示女性;

第18位数字是校检码,计算方法如下:

(1)将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。

(2)将这17位数字和系数相乘的结果相加。

(3)用加出来和除以11,取余数。

(4)余数只可能有0-1-2-3-4-5-6-7-8-9-10这11个数字。其分别对应的最后一位身份证的号码为1-0-X -9-8-7-6-5-4-3-2。(即余数0对应1,余数1对应0,余数2对应X…)

第一种方法:网页爬取身份证前六位

import urllib.request
from bs4 import BeautifulSoup
import re
import random
import time
import xlwt

# 通过爬取网页获取到身份证前六位
url = 'http://www.qucha.net/shenfenzheng/city.htm'
headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
}
request = urllib.request.Request(url, headers=headers) # 获取url的网页源码
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html, 'lxml')
strarr = []
for info in soup.find_all('td', valign='top'): # <td valign = "top"></td>中的内容
 pattern = re.compile(r'\d{6}') # 正则表达式,找6个整数
 pre = re.findall(pattern, info.text) # 在info中查找符合表达式的内容

def year():
 '''生成年份'''
 # 从1960开始算,now-18直接过滤掉小于18岁出生的年份
 now = time.strftime('%Y')
 second = random.randint(1960, int(now) - 18)
 return second


def month():
 '''生成月份'''
 three = str(random.randint(1, 12))
 mon = three.zfill(2)# zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0
 return mon


def day(year, month):
 '''生成日期'''
 four = str(getDay(year, month))
 days = four.zfill(2)
 return days


def getDay(year, month):
 '''根据传来的年月份返回日期'''
 # 1,3,5,7,8,10,12月为31天,4,6,9,11为30天,2月闰年为28天,其余为29天
 aday = 0
 if month in (1, 3, 5, 7, 8, 10, 12):
 aday = random.randint(1, 31)
 elif month in (4, 6, 9, 11):
 aday = random.randint(1, 30)
 else:
 # 即为2月判断是否为闰年
 if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
 aday = random.randint(1, 28)
 else:
 aday = random.randint(1, 29)
 return aday


def randoms():
 '''生成身份证后三位'''
 ran = str(random.randint(1, 999))
 five = ran.zfill(3)
 return five


# 前17位身份证
def ID():
 first = random.choice(pre)
 second = year()
 three = month()
 four = day(second, three)
 five = randoms()
 # 前17位身份证
 ID = str(first) + str(second) + three + four + five
 return ID

def ID_last():
 ID_17 = ID()
 lid = list(map(int, ID_17)) # 将字符串数组转为int列表
 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 权重项
 temp = 0
 for i in range(17):
 temp += lid[i]*weight[i]
 checkcode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']# 校验码映射
 ID_last = checkcode[temp%11]
 return ID_last

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('IDcard')
# 设置单元格宽度
worksheet.col(0).width = 5555

for i in range(100): #设置生成身份证号的数量
 IDcard = ID() + ID_last()
 worksheet.write(i, 0, IDcard)
 # 写入excel,参数对应 行, 列, 值
workbook.save('IDcard.xlsx')
# 运行后 会在当前目录生成一个IDcard.xlsx

第二种方法:身份证前六位从本地excel中取

如果自己有这么一份全国身份证前六位的数据且存在excel中,可以直接跳到第二步。没有的话,下面是爬取全国身份证前六位,并保存到自己本地的代码实现,建议跑一遍保存下来,谁知道这个爬取的地址哪天作者删除文件了呢,到时第一种方法就不适用了,得换地址处理等。(另外,爬取下来到excel中自己还能再处理一下前六位,因为我这个爬取包括“440000 广东省”这种,不知道身份证有没有前六位是这种的,我知道的好像没有,我爬下来的前六位没有删掉这些,如下图红框)

python批量生成身份证号到Excel的两种方法实例

# 通过爬取网页获取到身份证前六位并保存到本地excel中
import urllib.request
from bs4 import BeautifulSoup
import re
import xlwt

# 通过爬取网页获取到身份证前六位
url = 'http://www.qucha.net/shenfenzheng/city.htm'
headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
}
request = urllib.request.Request(url, headers=headers) # 获取url的网页源码
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html, 'lxml')
strarr = []
for info in soup.find_all('td', valign='top'): # <td valign = "top"></td>中的内容
 pattern = re.compile(r'\d{6}') # 正则表达式,找6个整数
 pre = re.findall(pattern, info.text) # 在info中查找符合表达式的内容,保存在pre中

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('ID_pre_six')
# 设置单元格宽度
worksheet.col(0).width = 3333

for i in range(len(pre)):
 worksheet.write(i, 0, pre[i])
 # 写入excel,参数对应 行, 列, 值
workbook.save('ID_pre_six.xlsx')
# 运行后 会在当前目录生成一个ID_pre_six.xlsx

导入本地excel数据(身份证前六位)保存为字符串数组,然后生成身份证号码

import random
import time
import xlwt
import pandas as pd

# 不把第1行作为列名,读取Excel那就没有列名,需增加参数:header=None
# 第一个参数为身份证前六位的excel数据路径
df = pd.read_excel('E:\Code\Python\ID_pre_six.xlsx', sheet_name='ID_pre_six', header=None)
# 获取最大行
nrows = df.shape[0]
pre = []
for iRow in range(nrows):
 # 将表中第一列数据写入pre数组中
 pre.append(df.iloc[iRow, 0])

def year():
 '''生成年份'''
 # 从1960开始算,now-18直接过滤掉小于18岁出生的年份
 now = time.strftime('%Y')
 second = random.randint(1960, int(now) - 18)
 return second


def month():
 '''生成月份'''
 three = str(random.randint(1, 12))
 mon = three.zfill(2)# zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0
 return mon


def day(year, month):
 '''生成日期'''
 four = str(getDay(year, month))
 days = four.zfill(2)
 return days


def getDay(year, month):
 '''根据传来的年月份返回日期'''
 # 1,3,5,7,8,10,12月为31天,4,6,9,11为30天,2月闰年为28天,其余为29天
 aday = 0
 if month in (1, 3, 5, 7, 8, 10, 12):
 aday = random.randint(1, 31)
 elif month in (4, 6, 9, 11):
 aday = random.randint(1, 30)
 else:
 # 即为2月判断是否为闰年
 if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
 aday = random.randint(1, 28)
 else:
 aday = random.randint(1, 29)
 return aday


def randoms():
 '''生成身份证后三位'''
 ran = str(random.randint(1, 999))
 five = ran.zfill(3)
 return five


# 前17位身份证
def ID():
 first = random.choice(pre)
 second = year()
 three = month()
 four = day(second, three)
 five = randoms()
 # 前17位身份证
 ID = str(first) + str(second) + three + four + five
 return ID

def ID_last():
 ID_17 = ID()
 lid = list(map(int, ID_17)) # 将字符串数组转为int列表
 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 权重项
 temp = 0
 for i in range(17):
 temp += lid[i]*weight[i]
 checkcode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']# 校验码映射
 ID_last = checkcode[temp%11]
 return ID_last

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('IDcard')
# 设置单元格宽度
worksheet.col(0).width = 5555

for i in range(100):# 设置生成数量
 IDcard = ID() + ID_last()
 worksheet.write(i, 0, IDcard)
 # 写入excel,参数对应 行, 列, 值
workbook.save('IDcard.xlsx')
# 运行后 会在当前目录生成一个IDcard.xlsx

PS:爬取网页中哪个tag里的内容,可以浏览器页面,右键->查看网页源代码,如下图,我需要的内容都含在方框那个tag里:

python批量生成身份证号到Excel的两种方法实例

参考:

总结

到此这篇关于python批量生成身份证号到Excel的两种方法的文章就介绍到这了,更多相关python批量生成身份证号到Excel内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python爬虫代理IP池实现方法
Jan 05 Python
Python编程之列表操作实例详解【创建、使用、更新、删除】
Jul 22 Python
python中requests库session对象的妙用详解
Oct 30 Python
Python实现读取txt文件并画三维图简单代码示例
Dec 09 Python
对python中array.sum(axis=?)的用法介绍
Jun 28 Python
pycharm 安装JPype的教程
Aug 08 Python
Python中IP地址处理IPy模块的方法
Aug 16 Python
Python GUI自动化实现绕过验证码登录
Jan 10 Python
python实现打砖块游戏
Feb 25 Python
python GUI库图形界面开发之PyQt5中QMainWindow, QWidget以及QDialog的区别和选择
Feb 26 Python
Python竟然能剪辑视频
May 25 Python
利用Python读取微信朋友圈的多种方法总结
Aug 23 Python
Django扫码抽奖平台的配置过程详解
Jan 14 #Python
如何用python实现一个HTTP连接池
Jan 14 #Python
如何用python写个模板引擎
Jan 14 #Python
opencv python 对指针仪表读数识别的两种方式
Jan 14 #Python
详解如何使用Pytest进行自动化测试
Jan 14 #Python
matplotlib对象拾取事件处理的实现
Jan 14 #Python
用python查找统一局域网下ip对应的mac地址
Jan 13 #Python
You might like
BBS(php &amp; mysql)完整版(五)
2006/10/09 PHP
PHP一些常用的正则表达式字符的一些转换
2008/07/29 PHP
PHP文章按日期(月日)SQL归档语句
2012/11/29 PHP
php对二维数组进行排序的简单实例
2013/12/19 PHP
PHP中浮点数计算比较及取整不准确的解决方法
2015/01/09 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
PHP微信支付结果通知与回调策略分析
2019/01/10 PHP
Prototype源码浅析 String部分(一)之有关indexOf优化
2012/01/15 Javascript
鼠标滑在标题上显示图片的JS代码
2013/11/19 Javascript
jQuery 无限级菜单的简单实例
2014/02/21 Javascript
浅谈JavaScript的事件
2015/02/27 Javascript
给easyui datebox扩展一个清空的实例
2016/11/09 Javascript
Bootstrap 模态框实例插件案例分析
2016/12/28 Javascript
jQuery实现别踩白块儿网页版小游戏
2017/01/18 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
基于jQuery对象和DOM对象和字符串之间的转化实例
2017/08/08 jQuery
EasyUI创建人员树的实例代码
2017/09/15 Javascript
WEB前端性能优化的7大手段详解
2020/02/04 Javascript
微信小程序实现watch监听
2020/06/04 Javascript
[03:42]2016国际邀请赛中国区预选赛首日现场玩家采访
2016/06/26 DOTA
浅谈Python的条件判断语句if/else语句
2019/03/21 Python
Django框架中间件定义与使用方法案例分析
2019/11/28 Python
详解如何在PyCharm控制台中输出彩色文字和背景
2020/08/17 Python
python 爬虫之selenium可视化爬虫的实现
2020/12/04 Python
利用python查看数组中的所有元素是否相同
2021/01/08 Python
CSS3教程(8):CSS3透明度指南
2009/04/02 HTML / CSS
Dr. Martens马汀博士德国官网:马丁靴鼻祖
2019/12/26 全球购物
中东最大的在线宠物店:Dubai Pet Food
2020/06/11 全球购物
英语系本科生个人求职信
2013/09/21 职场文书
文字自荐书范文
2014/02/10 职场文书
党的群众路线教育实践活动对照检查材料
2014/09/22 职场文书
公司领导九九重阳节发言稿2014
2014/09/25 职场文书
幼儿园见习报告
2014/10/30 职场文书
烟台的海导游词
2015/02/02 职场文书
运动会宣传稿50字
2015/07/23 职场文书
分布式架构Redis中有哪些数据结构及底层实现原理
2022/03/13 Redis