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

作者:书院小先生 时间:2023-05-25 07:42:11 

前言

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

来源:https://www.cnblogs.com/lee88888/p/12865590.html

标签:python,自动,生成
0
投稿

猜你喜欢

  • javascript获取select值的方法分析

    2024-04-19 09:50:26
  • javascript trim、left、right等函数,兼容IE,FireFox

    2009-09-18 14:55:00
  • 设置SQLServer数据库中某些表为只读的多种方法分享

    2024-01-23 06:27:47
  • PHP扩展之kafka安装应用案例详解

    2023-09-06 09:53:43
  • 简单理解PHP的面向对象编程方式

    2023-06-13 15:38:08
  • Mysql数据库存储过程基本语法讲解

    2024-01-20 04:46:35
  • mysql之动态增添字段实现方式

    2024-01-27 13:09:23
  • Scrapy 配置动态代理IP的实现

    2023-12-10 19:06:22
  • 判断字段是否被更新 新旧数据写入Audit Log表中

    2012-01-29 17:56:33
  • 解决python 找不到module的问题

    2022-08-05 07:33:25
  • Apache2.4.x版wampserver本地php服务器如何让外网访问及启用.htaccess

    2023-11-04 09:27:00
  • Perl中的符号 ->;、=>; 和 :: 分别表示什么意思?

    2022-11-28 14:38:48
  • SQL语句解析执行的过程及原理

    2024-01-26 21:11:55
  • 纯Python开发的nosql数据库CodernityDB介绍和使用实例

    2022-10-21 12:16:05
  • 解析mysql修改为utf8后仍然有乱码的问题

    2024-01-14 14:36:09
  • 一文带你吃透什么是PHP中的序列化

    2023-06-12 19:44:20
  • Mysql事务的隔离级别(脏读+幻读+可重复读)

    2024-01-29 07:45:06
  • Python获取android设备cpu和内存占用情况

    2023-02-25 20:46:47
  • django模型类中,null=True,blank=True用法说明

    2022-05-29 00:47:31
  • Python中的turtle画箭头,矩形,五角星

    2023-11-22 13:38:12
  • asp之家 网络编程 m.aspxhome.com