python批量生成身份證號到Excel的兩種方法實例
身份證號碼的編排規(guī)則
前1、2位數(shù)字表示:所在省份的代碼;
第3、4位數(shù)字表示:所在城市的代碼;
第5、6位數(shù)字表示:所在區(qū)縣的代碼;
第7~14位數(shù)字表示:出生年、月、日;
第15、16位數(shù)字表示:所在地的派出所的代碼;
第17位數(shù)字表示性別:奇數(shù)表示男性,偶數(shù)表示女性;
第18位數(shù)字是校檢碼,計算方法如下:
(1)將前面的身份證號碼17位數(shù)分別乘以不同的系數(shù)。從第一位到第十七位的系數(shù)分別為:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
(2)將這17位數(shù)字和系數(shù)相乘的結(jié)果相加。
(3)用加出來和除以11,取余數(shù)。
(4)余數(shù)只可能有0-1-2-3-4-5-6-7-8-9-10這11個數(shù)字。其分別對應的最后一位身份證的號碼為1-0-X -9-8-7-6-5-4-3-2。(即余數(shù)0對應1,余數(shù)1對應0,余數(shù)2對應X…)
第一種方法:網(wǎng)頁爬取身份證前六位
import urllib.request
from bs4 import BeautifulSoup
import re
import random
import time
import xlwt
# 通過爬取網(wǎng)頁獲取到身份證前六位
url = 'http://www.qucha.net/shenfenzheng/city.htm'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
}
request = urllib.request.Request(url, headers=headers) # 獲取url的網(wǎng)頁源碼
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html, 'lxml')
strarr = []
for info in soup.find_all('td', valign='top'): # <td valign = "top"></td>中的內(nèi)容
pattern = re.compile(r'\d{6}') # 正則表達式,找6個整數(shù)
pre = re.findall(pattern, info.text) # 在info中查找符合表達式的內(nèi)容
def year():
'''生成年份'''
# 從1960開始算,now-18直接過濾掉小于18歲出生的年份
now = time.strftime('%Y')
second = random.randint(1960, int(now) - 18)
return second
def month():
'''生成月份'''
three = str(random.randint(1, 12))
mon = three.zfill(2)# zfill() 方法返回指定長度的字符串,原字符串右對齊,前面填充0
return mon
def day(year, month):
'''生成日期'''
four = str(getDay(year, month))
days = four.zfill(2)
return days
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():
'''生成身份證后三位'''
ran = str(random.randint(1, 999))
five = ran.zfill(3)
return five
# 前17位身份證
def ID():
first = random.choice(pre)
second = year()
three = month()
four = day(second, three)
five = randoms()
# 前17位身份證
ID = str(first) + str(second) + three + four + five
return ID
def ID_last():
ID_17 = ID()
lid = list(map(int, ID_17)) # 將字符串數(shù)組轉(zhuǎn)為int列表
weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 權(quán)重項
temp = 0
for i in range(17):
temp += lid[i]*weight[i]
checkcode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']# 校驗碼映射
ID_last = checkcode[temp%11]
return ID_last
# 創(chuàng)建一個workbook 設置編碼
workbook = xlwt.Workbook(encoding='utf-8')
# 創(chuàng)建一個worksheet
worksheet = workbook.add_sheet('IDcard')
# 設置單元格寬度
worksheet.col(0).width = 5555
for i in range(100): #設置生成身份證號的數(shù)量
IDcard = ID() + ID_last()
worksheet.write(i, 0, IDcard)
# 寫入excel,參數(shù)對應 行, 列, 值
workbook.save('IDcard.xlsx')
# 運行后 會在當前目錄生成一個IDcard.xlsx
第二種方法:身份證前六位從本地excel中取
如果自己有這么一份全國身份證前六位的數(shù)據(jù)且存在excel中,可以直接跳到第二步。沒有的話,下面是爬取全國身份證前六位,并保存到自己本地的代碼實現(xiàn),建議跑一遍保存下來,誰知道這個爬取的地址哪天作者刪除文件了呢,到時第一種方法就不適用了,得換地址處理等。(另外,爬取下來到excel中自己還能再處理一下前六位,因為我這個爬取包括“440000 廣東省”這種,不知道身份證有沒有前六位是這種的,我知道的好像沒有,我爬下來的前六位沒有刪掉這些,如下圖紅框)

# 通過爬取網(wǎng)頁獲取到身份證前六位并保存到本地excel中
import urllib.request
from bs4 import BeautifulSoup
import re
import xlwt
# 通過爬取網(wǎng)頁獲取到身份證前六位
url = 'http://www.qucha.net/shenfenzheng/city.htm'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
}
request = urllib.request.Request(url, headers=headers) # 獲取url的網(wǎng)頁源碼
response = urllib.request.urlopen(request)
html = response.read()
soup = BeautifulSoup(html, 'lxml')
strarr = []
for info in soup.find_all('td', valign='top'): # <td valign = "top"></td>中的內(nèi)容
pattern = re.compile(r'\d{6}') # 正則表達式,找6個整數(shù)
pre = re.findall(pattern, info.text) # 在info中查找符合表達式的內(nèi)容,保存在pre中
# 創(chuàng)建一個workbook 設置編碼
workbook = xlwt.Workbook(encoding='utf-8')
# 創(chuàng)建一個worksheet
worksheet = workbook.add_sheet('ID_pre_six')
# 設置單元格寬度
worksheet.col(0).width = 3333
for i in range(len(pre)):
worksheet.write(i, 0, pre[i])
# 寫入excel,參數(shù)對應 行, 列, 值
workbook.save('ID_pre_six.xlsx')
# 運行后 會在當前目錄生成一個ID_pre_six.xlsx
導入本地excel數(shù)據(jù)(身份證前六位)保存為字符串數(shù)組,然后生成身份證號碼
import random
import time
import xlwt
import pandas as pd
# 不把第1行作為列名,讀取Excel那就沒有列名,需增加參數(shù):header=None
# 第一個參數(shù)為身份證前六位的excel數(shù)據(jù)路徑
df = pd.read_excel('E:\Code\Python\ID_pre_six.xlsx', sheet_name='ID_pre_six', header=None)
# 獲取最大行
nrows = df.shape[0]
pre = []
for iRow in range(nrows):
# 將表中第一列數(shù)據(jù)寫入pre數(shù)組中
pre.append(df.iloc[iRow, 0])
def year():
'''生成年份'''
# 從1960開始算,now-18直接過濾掉小于18歲出生的年份
now = time.strftime('%Y')
second = random.randint(1960, int(now) - 18)
return second
def month():
'''生成月份'''
three = str(random.randint(1, 12))
mon = three.zfill(2)# zfill() 方法返回指定長度的字符串,原字符串右對齊,前面填充0
return mon
def day(year, month):
'''生成日期'''
four = str(getDay(year, month))
days = four.zfill(2)
return days
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():
'''生成身份證后三位'''
ran = str(random.randint(1, 999))
five = ran.zfill(3)
return five
# 前17位身份證
def ID():
first = random.choice(pre)
second = year()
three = month()
four = day(second, three)
five = randoms()
# 前17位身份證
ID = str(first) + str(second) + three + four + five
return ID
def ID_last():
ID_17 = ID()
lid = list(map(int, ID_17)) # 將字符串數(shù)組轉(zhuǎn)為int列表
weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 權(quán)重項
temp = 0
for i in range(17):
temp += lid[i]*weight[i]
checkcode = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']# 校驗碼映射
ID_last = checkcode[temp%11]
return ID_last
# 創(chuàng)建一個workbook 設置編碼
workbook = xlwt.Workbook(encoding='utf-8')
# 創(chuàng)建一個worksheet
worksheet = workbook.add_sheet('IDcard')
# 設置單元格寬度
worksheet.col(0).width = 5555
for i in range(100):# 設置生成數(shù)量
IDcard = ID() + ID_last()
worksheet.write(i, 0, IDcard)
# 寫入excel,參數(shù)對應 行, 列, 值
workbook.save('IDcard.xlsx')
# 運行后 會在當前目錄生成一個IDcard.xlsx
PS:爬取網(wǎng)頁中哪個tag里的內(nèi)容,可以瀏覽器頁面,右鍵->查看網(wǎng)頁源代碼,如下圖,我需要的內(nèi)容都含在方框那個tag里:

參考:
http://www.dbjr.com.cn/article/204089.htm
總結(jié)
到此這篇關于python批量生成身份證號到Excel的兩種方法的文章就介紹到這了,更多相關python批量生成身份證號到Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- python基于opencv批量生成驗證碼的示例
- python用faker庫批量生成假數(shù)據(jù)
- 如何使用python-opencv批量生成帶噪點噪線的數(shù)字驗證碼
- Python如何批量生成和調(diào)用變量
- python利用faker庫批量生成測試數(shù)據(jù)
- python批量生成條形碼的示例
- Python操作Word批量生成合同的實現(xiàn)示例
- 利用Python腳本批量生成SQL語句
- 基于Python批量生成指定尺寸縮略圖代碼實例
- Python3批量生成帶logo的二維碼方法
- Python批量生成幻影坦克圖片實例代碼
- 利用Python批量生成任意尺寸的圖片
- python批量生成本地ip地址的方法
- 教你使用Python根據(jù)模板批量生成docx文檔
相關文章
Python?tkinter中l(wèi)abel控件動態(tài)改變值問題
這篇文章主要介紹了Python?tkinter中l(wèi)abel控件動態(tài)改變值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Python?copy()與deepcopy()方法之間有什么區(qū)別
這篇文章主要介紹了Python中的copy()和deepcopy(),下面詳細介紹該內(nèi)容并附上詳細代碼,需要的朋友可以參考一下文章的具體內(nèi)容,希望對你有所幫助2022-10-10
opencv-python圖像配準(匹配和疊加)的實現(xiàn)
圖像配準需是指對不同條件下得到的兩幅或多幅圖像進行匹配、疊加的過程。本文詳細的介紹了如何使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
python批量將PDF文件轉(zhuǎn)換成圖片的實現(xiàn)代碼
這篇文章使用python編寫了一個小腳本,目的是為了實現(xiàn)批量將PDF文件轉(zhuǎn)換成圖片,文中有詳細的實現(xiàn)代碼,對我們的學習或工作有一定的幫助,感興趣的小伙伴可以參考閱讀一下2023-08-08

