python自动生成证件号的方法示例


Posted in Python onJanuary 14, 2021

前言

在跟进需求的时候,往往涉及到测试,特别是需要用到身份信息的时候,总绕不开身份证号码这个话题。之前在跟一个互联网产品的时候,需要很多身份证做测试,又不想装太多软件自动生成(有需要的小伙伴可自行搜索身份证号码自动生成软件),按照身份证规则现编也比较浪费时间,在处理身份数据时,Python就非常有用了。

方法示例如下

# Author:BeeLe
# -*-coding:utf-8-*-

# 生成身份证号码主程序
import urllib.request
import requests
from bs4 import BeautifulSoup
import re
import random
import time
import lxml


# class IDCard():
def regiun(strarr):
 '''生成身份证前六位'''
 first = random.choice(strarr)
 return first


def year():
 '''生成年份'''
 # 1978为第一代身份证执行年份,now-18直接过滤掉小于18岁出生的年份
 now = time.strftime('%Y')
 second = random.randint(1978, int(now) - 18)
 # age = int(now)-second
 # print('随机生成的身份证人员年龄为:'+str(age))
 return second


def month():
 '''生成月份'''
 three = random.randint(1, 12)
 if three < 10:
 three = '0' + str(three)
 return three
 else:
 return three


def day(year, month):
 '''生成日期'''
 four = getDay(year, month)
 # 日期小于10以下,前面加上0填充
 if four < 10:
 four = '0' + str(four)
 return four
 return four


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():
 '''生成身份证后四位'''
 five = random.randint(1, 9999)
 if five < 10:
 five = '000' + str(five)
 elif 10 < five < 100:
 five = '00' + str(five)
 elif 100 < five < 1000:
 five = '0' + str(five)
 return five

# if __name__ == '__main__':

def idcard():
 # 通过爬取网页获取到身份证前六位
 url = 'https://wenku.baidu.com/view/a55406b919e8b8f67d1cb920'
 request = urllib.request.Request(url) # 获取url的网页源码
 response = urllib.request.urlopen(request)
 html = response.read()
 soup = BeautifulSoup(html, 'lxml')
 strarr = []
 for info in soup.find_all(class_='expanded'):
 pattern = re.compile(r'\d{6}')
 b = re.findall(pattern, info.text)
 for item in b:
  strarr.append(item)

 for i in range(1, 2):
 first = regiun(strarr)
 second = year()
 three = month()
 four = day(second, three)
 last = randoms()
 IDCard = str(first) + str(second) + str(three) + str(four) + str(last)
 # print('随机生成的身份证号码为:' + IDCard)
 return IDCard
# Idcard = idcard

总结

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

Python 相关文章推荐
精确查找PHP WEBSHELL木马的方法(1)
Apr 12 Python
Python中的各种装饰器详解
Apr 11 Python
Python的collections模块中的OrderedDict有序字典
Jul 07 Python
用yum安装MySQLdb模块的步骤方法
Dec 15 Python
Python面向对象之类的内置attr属性示例
Dec 14 Python
Python类装饰器实现方法详解
Dec 21 Python
两个元祖T1=('a', 'b'),T2=('c', 'd')使用匿名函数将其转变成[{'a': 'c'},{'b': 'd'}]的几种方法
Mar 05 Python
关于Python3 lambda函数的深入浅出
Nov 27 Python
Python读取VOC中的xml目标框实例
Mar 10 Python
python3中sorted函数里cmp参数改变详解
Mar 12 Python
django haystack实现全文检索的示例代码
Jun 24 Python
关于Python中进度条的六个实用技巧分享
Apr 05 Python
用python批量移动文件
Jan 14 #Python
python用700行代码实现http客户端
Jan 14 #Python
python批量生成身份证号到Excel的两种方法实例
Jan 14 #Python
Django扫码抽奖平台的配置过程详解
Jan 14 #Python
如何用python实现一个HTTP连接池
Jan 14 #Python
如何用python写个模板引擎
Jan 14 #Python
opencv python 对指针仪表读数识别的两种方式
Jan 14 #Python
You might like
PHP 数组教程 定义数组
2009/10/23 PHP
MySQL 日期时间函数常用总结
2012/06/12 PHP
使用PHP接收POST数据,解析json数据
2013/06/28 PHP
thinkphp文件处理类Dir.class.php的用法分析
2014/12/08 PHP
iis6手工创建网站后无法运行php脚本的解决方法
2017/06/08 PHP
php array_map()函数实例用法
2021/03/03 PHP
jQuery Tools tab使用介绍
2012/07/14 Javascript
JS中令人发指的valueOf方法介绍
2013/02/22 Javascript
jquery右下角弹出提示框示例代码
2013/10/08 Javascript
各种常用的JS函数整理
2013/10/25 Javascript
jQuery中change事件用法实例
2014/12/26 Javascript
javascript+canvas制作九宫格小程序
2014/12/28 Javascript
js改变embed标签src值的方法
2015/04/10 Javascript
javascript实现禁止鼠标滚轮事件
2015/07/24 Javascript
WordPress中鼠标悬停显示和隐藏评论及引用按钮的实现
2016/01/12 Javascript
JS 数字转换为大写金额的简单实例
2016/08/04 Javascript
原生nodejs使用websocket代码分享
2018/04/07 NodeJs
JS编写兼容IE6,7,8浏览器无缝自动轮播
2018/10/12 Javascript
[01:15:45]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第一局
2016/02/26 DOTA
Python 调用VC++的动态链接库(DLL)
2008/09/06 Python
python 链接和操作 memcache方法
2017/03/04 Python
Python读取MRI并显示为灰度图像实例代码
2018/01/03 Python
python多进程提取处理大量文本的关键词方法
2018/06/05 Python
Python面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】
2019/01/05 Python
Python基于Tensor FLow的图像处理操作详解
2020/01/15 Python
python 实现字符串下标的输出功能
2020/02/13 Python
德国街头和运动文化高品质商店:BSTN Store
2017/08/26 全球购物
Hotter Shoes英国官网:英伦风格,舒适的鞋子
2017/12/28 全球购物
严选全球尖货,立足香港:Bonpont宝盆
2018/07/24 全球购物
基层干部2014全国两会学习心得体会
2014/03/10 职场文书
我的中国梦口号
2014/06/16 职场文书
搞笑老公保证书
2015/02/26 职场文书
给朋友的赠语
2015/06/23 职场文书
吧主申请感言怎么写
2015/08/03 职场文书
Python实现归一化算法详情
2022/03/18 Python
Matplotlib绘制条形图的方法你知道吗
2022/03/21 Python