python自動(dòng)生成證件號(hào)的方法示例
前言
在跟進(jìn)需求的時(shí)候,往往涉及到測(cè)試,特別是需要用到身份信息的時(shí)候,總繞不開身份證號(hào)碼這個(gè)話題。之前在跟一個(gè)互聯(lián)網(wǎng)產(chǎn)品的時(shí)候,需要很多身份證做測(cè)試,又不想裝太多軟件自動(dòng)生成(有需要的小伙伴可自行搜索身份證號(hào)碼自動(dòng)生成軟件),按照身份證規(guī)則現(xiàn)編也比較浪費(fèi)時(shí)間,在處理身份數(shù)據(jù)時(shí),Python就非常有用了。
方法示例如下
# Author:BeeLe # -*-coding:utf-8-*- # 生成身份證號(hào)碼主程序 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為第一代身份證執(zhí)行年份,now-18直接過濾掉小于18歲出生的年份 now = time.strftime('%Y') second = random.randint(1978, int(now) - 18) # age = int(now)-second # print('隨機(jī)生成的身份證人員年齡為:'+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): '''根據(jù)傳來的年月份返回日期''' # 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(): # 通過爬取網(wǎng)頁獲取到身份證前六位 url = 'https://wenku.baidu.com/view/a55406b919e8b8f67d1cb920' request = urllib.request.Request(url) # 獲取url的網(wǎng)頁源碼 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('隨機(jī)生成的身份證號(hào)碼為:' + IDCard) return IDCard # Idcard = idcard
總結(jié)
到此這篇關(guān)于python自動(dòng)生成證件號(hào)的文章就介紹到這了,更多相關(guān)python自動(dòng)生成證件號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python網(wǎng)絡(luò)爬蟲實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Python網(wǎng)絡(luò)爬蟲實(shí)例,爬蟲的定義、主要框架等基礎(chǔ)概念,感興趣的小伙伴們可以參考一下2016-04-04python實(shí)現(xiàn)學(xué)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)學(xué)員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Python實(shí)現(xiàn)按特定格式對(duì)文件進(jìn)行讀寫的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)按特定格式對(duì)文件進(jìn)行讀寫的方法,可實(shí)現(xiàn)文件按原有格式讀取與寫入的功能,涉及文件的讀取、遍歷、轉(zhuǎn)換、寫入等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Python機(jī)器學(xué)習(xí)應(yīng)用之支持向量機(jī)的分類預(yù)測(cè)篇
最近完成的一個(gè)項(xiàng)目用到了SVM,之前也一直有聽說支持向量機(jī),知道它是機(jī)器學(xué)習(xí)中一種非常厲害的算法。利用將近一個(gè)星期的時(shí)間學(xué)習(xí)了一下支持向量機(jī),把原理推了一遍,感覺支持向量機(jī)確實(shí)挺厲害的,這篇文章帶你了解它2022-01-01