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 相关文章推荐
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
Jun 18 Python
django反向解析URL和URL命名空间的方法
Jun 05 Python
python将txt文档每行内容循环插入数据库的方法
Dec 28 Python
对python多线程中互斥锁Threading.Lock的简单应用详解
Jan 11 Python
用Python中的turtle模块画图两只小羊方法
Apr 09 Python
Python面向对象程序设计构造函数和析构函数用法分析
Apr 12 Python
python可视化爬虫界面之天气查询
Jul 03 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
Aug 21 Python
Python 3.8正式发布,来尝鲜这些新特性吧
Oct 15 Python
Django-simple-captcha验证码包使用方法详解
Nov 28 Python
python numpy中setdiff1d的用法说明
Apr 22 Python
Python预测分词的实现
Jun 18 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的FTP学习(四)
2006/10/09 PHP
PHP 上传文件大小限制
2009/07/05 PHP
PHP错误提示的关闭方法详解
2013/06/23 PHP
解析php扩展php_curl.dll不加载的解决方法
2013/06/26 PHP
php中curl和file_get_content的区别
2014/05/10 PHP
php.ini save_handler 修改不生效的解决办法
2014/07/22 PHP
PHP日期函数date格式化UNIX时间的方法
2015/03/19 PHP
Yii2实现UploadedFile上传文件示例
2017/02/15 PHP
jquery关于图形报表的运用实现代码
2011/01/06 Javascript
仿百度联盟对联广告实现代码
2014/08/30 Javascript
jQuery构造函数init参数分析续
2015/05/13 Javascript
js实现select跳转菜单新窗口效果代码分享(超简单)
2015/08/21 Javascript
jQuery实现获取元素索引值index的方法
2016/09/18 Javascript
react.js使用webpack搭配环境的入门教程
2017/08/14 Javascript
vue+vuex+axios+echarts画一个动态更新的中国地图的方法
2017/12/19 Javascript
解决Vue-cli npm run build生产环境打包,本地不能打开的问题
2018/09/20 Javascript
实例讲解v-if和v-show的区别
2019/01/31 Javascript
详解如何写出一个利于扩展的vue路由配置
2019/05/16 Javascript
详解使用WebPack搭建React开发环境
2019/08/06 Javascript
[01:18]一目了然!DOTA2DotA快捷操作对比第一弹
2014/07/01 DOTA
haskell实现多线程服务器实例代码
2013/11/26 Python
跟老齐学Python之深入变量和引用对象
2014/09/24 Python
Python中使用partial改变方法默认参数实例
2015/04/28 Python
python下MySQLdb用法实例分析
2015/06/08 Python
python3之模块psutil系统性能信息使用
2018/05/30 Python
python 图片去噪的方法示例
2019/07/09 Python
Django 拆分model和view的实现方法
2019/08/16 Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
2020/04/14 Python
详解pyinstaller生成exe的闪退问题解决方案
2020/06/19 Python
如何卸载python插件
2020/07/08 Python
萨克斯第五大道精品百货店: Saks Fifth Avenue
2017/04/28 全球购物
男女钓鱼靴和甲板鞋:XTRATUF
2021/01/09 全球购物
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
社会工作专业求职信
2014/07/15 职场文书
2016年寒假社会实践活动心得体会
2015/10/09 职场文书
幼师自荐信范文(2016推荐篇)
2016/01/28 职场文书