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 相关文章推荐
Python自动重试HTTP连接装饰器
Apr 28 Python
Python使用修饰器执行函数的参数检查功能示例
Sep 26 Python
Python中XlsxWriter模块简介与用法分析
Apr 24 Python
Tensorflow分类器项目自定义数据读入的实现
Feb 05 Python
浅谈django2.0 ForeignKey参数的变化
Aug 06 Python
Python文件操作函数用法实例详解
Dec 24 Python
为什么黑客都用python(123个黑客必备的Python工具)
Jan 31 Python
解决Python import docx出错DLL load failed的问题
Feb 13 Python
Python如何将图像音视频等资源文件隐藏在代码中(小技巧)
Feb 16 Python
python脚本和网页有何区别
Jul 02 Python
Python 解析简单的XML数据
Jul 24 Python
python基于scrapy爬取京东笔记本电脑数据并进行简单处理和分析
Apr 14 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
上海永华YH-R296(华普R-96)12波段立体声收音机的分析和打理
2021/03/02 无线电
解析php中eclipse 用空格替换 tab键
2013/06/24 PHP
thinkphp在模型中自动完成session赋值示例代码
2014/09/09 PHP
详解php设置session(过期、失效、有效期)
2015/11/12 PHP
JavaScript XML实现两级级联下拉列表
2008/11/10 Javascript
JavaScript 利用StringBuffer类提升+=拼接字符串效率
2009/11/24 Javascript
JS对img进行操作(换图片/切图/轮换/停止)
2013/04/17 Javascript
Javascript学习笔记之 对象篇(一) : 对象的使用和属性
2014/06/24 Javascript
ECMAScript6中Map/WeakMap详解
2015/06/12 Javascript
Javascript函数式编程语言
2015/10/11 Javascript
JS中call/apply、arguments、undefined/null方法详解
2016/02/15 Javascript
Bootstrap图片轮播组件Carousel使用方法详解
2016/10/20 Javascript
实例浅析js的this
2016/12/11 Javascript
微信小程序(三):网络请求
2017/01/13 Javascript
angular4 JavaScript内存溢出问题
2018/03/06 Javascript
ES6基础之数组和对象的拓展实例详解
2019/08/22 Javascript
js+css实现全屏侧边栏
2020/06/16 Javascript
Python中函数的用法实例教程
2014/09/08 Python
python简单实现基于SSL的IRC bot实例
2015/06/15 Python
Windows中安装使用Virtualenv来创建独立Python环境
2016/05/31 Python
Python实现信用卡系统(支持购物、转账、存取钱)
2016/06/24 Python
简单了解Python3里的一些新特性
2019/07/13 Python
Python判断字符串是否xx开始或结尾的示例
2019/08/08 Python
Python3并发写文件与Python对比
2019/11/20 Python
OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现
2019/11/25 Python
python代码能做成软件吗
2020/07/24 Python
python代数式括号有效性检验示例代码
2020/10/04 Python
师范院校学生自荐信范文
2013/12/27 职场文书
快递业务员岗位职责
2014/01/06 职场文书
社会保险接收函
2014/01/12 职场文书
小学生新学期寄语
2014/01/19 职场文书
英语专业个人求职信范文
2014/02/01 职场文书
会计电算化学生个人的自我评价
2014/02/08 职场文书
企业演讲比赛主持词
2014/03/18 职场文书
党员演讲稿
2014/09/04 职场文书
让子弹飞观后感
2015/06/11 职场文书