python利用faker庫(kù)批量生成測(cè)試數(shù)據(jù)
安裝
pip install faker
使用
簡(jiǎn)單使用
本庫(kù)可生成姓名、地址、電話、郵箱、公司等等一系列數(shù)據(jù)。首先導(dǎo)入庫(kù),實(shí)例化:
from faker import Faker fake = Faker()
先看看正面生成一個(gè)人的姓名地址吧:
for _ in range(10): print(fake.name())
rs. Elizabeth Carter MD Mark Obrien Madeline Oliver Ruth Newman Lori Bennett Victor Nolan Bethany Mitchell Steven Henderson Shannon Burke Christopher Pacheco Morgan Hernandez
有時(shí)候我們并不想要英文數(shù)據(jù),faker
庫(kù)支持中文數(shù)據(jù),只需要設(shè)置一下。
from faker import Faker fake = Faker(["zh_CN"])
zh_CN
即代表中文。
需求1
了解一下都有哪些屬性:
print(fake.name()) #姓名 print(fake.address()) # 地址 print(fake.phone_number()) #電話 print(fake.email()) #郵箱 print(fake.company()) #公司 print(fake.job()) # 職位 print(fake.building_number()) #哪棟樓 print(fake.city()) # 城市
這時(shí)候小編需要幾萬(wàn)個(gè)假人的個(gè)人信息數(shù)據(jù),就可以這樣生成:
from faker import Faker fake = Faker(["zh_CN"]) Faker.seed(0) def get_person(): key_list = ["姓名","電話","郵箱","地址","工作單位","職位","城市","銀行卡號(hào)"] for _ in range(20000): info_list = [fake.name(),fake.phone_number(),fake.email(),fake.address(),fake.company(),fake.job(),fake.city(),fake.credit_card_number()] person_info = dict(zip(key_list,info_list)) print(person_info)
生成如下數(shù)據(jù)(只截取了部分):
{'姓名': '廖紅霞', '電話': '15087647593', '郵箱': 'xiasong@gmail.com', '地址': '澳門(mén)特別行政區(qū)麗華市崇文馬街F座 187784', '工作單位': '立信電子科技有限公司', '職位': '調(diào)研員', '城市': '大冶縣', '銀行卡號(hào)': '630453513933'} {'姓名': '柏秀梅', '電話': '18215871484', '郵箱': 'xiulan83@hotmail.com', '地址': '廣東省興城市花溪銀川路y座 722018', '工作單位': '數(shù)字100信息有限公司', '職位': '保險(xiǎn)精算師', '城市': '張家港縣', '銀行卡號(hào)': '347515917953308'} {'姓名': '李龍', '電話': '18656012309', '郵箱': 'dshen@gmail.com', '地址': '青海省沈陽(yáng)縣龍?zhí)逗仙铰稬座 508691', '工作單位': '濟(jì)南億次元網(wǎng)絡(luò)有限公司', '職位': '運(yùn)輸經(jīng)理/主管', '城市': '香港縣', '銀行卡號(hào)': '213145792302255'} {'姓名': '李桂香', '電話': '14507698456', '郵箱': 'nacai@hotmail.com', '地址': '上海市太原縣南長(zhǎng)柳州路L座 661093', '工作單位': '思優(yōu)網(wǎng)絡(luò)有限公司', '職位': '電腦操作員/打字員', '城市': '秀芳市', '銀行卡號(hào)': '4027142787890079'} {'姓名': '龍杰', '電話': '14563812066', '郵箱': 'weichao@qiandeng.cn', '地址': '新疆維吾爾自治區(qū)六盤(pán)水市普陀長(zhǎng)沙街S座 185124', '工作單位': '創(chuàng)匯網(wǎng)絡(luò)有限公司', '職位': '飛行器設(shè)計(jì)與制造', '城市': '沈陽(yáng)縣', '銀行卡號(hào)': '4977658236940223'} {'姓名': '楊坤', '電話': '13690042294', '郵箱': 'fdu@zhangjiang.net', '地址': '黑龍江省建軍市白云荊門(mén)路g座 704522', '工作單位': '通際名聯(lián)科技有限公司', '職位': '美容顧問(wèn)', '城市': '濟(jì)南市', '銀行卡號(hào)': '2284779361534920'} {'姓名': '黃超', '電話': '13308731764', '郵箱': 'zhengjie@kt.cn', '地址': '澳門(mén)特別行政區(qū)宇市南溪東莞街z座 638930', '工作單位': '信誠(chéng)致遠(yuǎn)網(wǎng)絡(luò)有限公司', '職位': '研發(fā)總監(jiān)/部長(zhǎng)/專(zhuān)家', '城市': '秀芳縣', '銀行卡號(hào)': '2244711801320403'} {'姓名': '徐麗麗', '電話': '14575868809', '郵箱': 'fpeng@63.cn', '地址': '江西省雪梅縣新城王路p座 751744', '工作單位': '明騰科技有限公司', '職位': '機(jī)械機(jī)床', '城市': '馬鞍山市', '銀行卡號(hào)': '3527912560976700'} {'姓名': '石磊', '電話': '13125185367', '郵箱': 'wanqiang@qiangyan.cn', '地址': '青海省巖市朝陽(yáng)李路b座 153751', '工作單位': '鑫博騰飛網(wǎng)絡(luò)有限公司', '職位': '網(wǎng)站運(yùn)營(yíng)專(zhuān)員', '城市': '桂蘭縣', '銀行卡號(hào)': '4886841269611610'} {'姓名': '金淑珍', '電話': '13476607541', '郵箱': 'tianjun@rl.cn', '地址': '內(nèi)蒙古自治區(qū)淮安縣永川海門(mén)路h座 475420', '工作單位': '雨林木風(fēng)計(jì)算機(jī)網(wǎng)絡(luò)有限公司', '職位': '數(shù)控編程', '城市': '晶市', '銀行卡號(hào)': '3594262235833243'} {'姓名': '田麗麗', '電話': '13261126486', '郵箱': 'liuqiang@gmail.com', '地址': '山東省濤市魏都鄭州街X座 841892', '工作單位': '菊風(fēng)公司網(wǎng)絡(luò)有限公司', '職位': '首席財(cái)務(wù)官 CFO', '城市': '雷縣', '銀行卡號(hào)': '4173407970341489262'} {'姓名': '馬紅梅', '電話': '18538640140', '郵箱': 'yangwang@wu.cn', '地址': '海南省長(zhǎng)春市沙市深圳路d座 448298', '工作單位': '畢博誠(chéng)信息有限公司', '職位': '漿紗工', '城市': '秀云市', '銀行卡號(hào)': '4211669728457637780'} {'姓名': '蔡濤', '電話': '15270930576', '郵箱': 'pengfang@hotmail.com', '地址': '陜西省偉市懷柔蘇路N座 316756', '工作單位': '萬(wàn)迅電腦信息有限公司', '職位': '銷(xiāo)售助理', '城市': '宇市', '銀行卡號(hào)': '4566763373906032'} {'姓名': '孔彬', '電話': '18523949184', '郵箱': 'clin@hotmail.com', '地址': '海南省淑珍市蕭山昆明街s座 702163', '工作單位': '思優(yōu)科技有限公司', '職位': '幕墻工程師', '城市': '平市', '銀行卡號(hào)': '6540803513853342'} {'姓名': '盧晨', '電話': '13347530481', '郵箱': 'minzeng@ming.cn', '地址': '江蘇省浩縣沙灣羅路X座 732455', '工作單位': '四通信息有限公司', '職位': '酒店前臺(tái)', '城市': '桂珍縣', '銀行卡號(hào)': '374301912633401'} {'姓名': '林麗麗', '電話': '13741953843', '郵箱': 'pcheng@gmail.com', '地址': '湖南省波市上街秦路j座 423793', '工作單位': '合聯(lián)電子傳媒有限公司', '職位': '加油站工作員', '城市': '鳳英縣', '銀行卡號(hào)': '2689577403827786'} {'姓名': '毛建華', '電話': '13235460305', '郵箱': 'wkong@hotmail.com', '地址': '江蘇省建軍市牧野上海路c座 351056', '工作單位': '創(chuàng)匯網(wǎng)絡(luò)有限公司', '職位': '叉車(chē)/鏟車(chē)工', '城市': '昆明縣', '銀行卡號(hào)': '213164887091665'} {'姓名': '胡玉珍', '電話': '14521532302', '郵箱': 'lqiu@yahoo.com', '地址': '重慶市云縣秀英吳路z座 327385', '工作單位': '天開(kāi)網(wǎng)絡(luò)有限公司', '職位': '牙科醫(yī)生', '城市': '成都縣', '銀行卡號(hào)': '6589840761657001'} {'姓名': '亓建華', '電話': '15543885643', '郵箱': 'xxu@jb.net', '地址': '新疆維吾爾自治區(qū)佛山縣花溪南寧街z座 162029', '工作單位': '創(chuàng)億科技有限公司', '職位': '情報(bào)信息分析人員', '城市': '慧縣', '銀行卡號(hào)': '4186428212917'} {'姓名': '曾萍', '電話': '15864440644', '郵箱': 'yan36@hotmail.com', '地址': '陜西省淮安縣白云鄭街P座 605823', '工作單位': '維旺明信息有限公司', '職位': '生產(chǎn)/營(yíng)運(yùn)', '城市': '成縣', '銀行卡號(hào)': '3573781327166449'}
還是可以的吧。
需求2
小編需要一些網(wǎng)站信息數(shù)據(jù),先看一下屬性
print(fake.domain_name())#域名 print(fake.image_url())#圖片鏈接 print(fake.uri()) #資源 print(fake.url()) #url print(fake.ipv4())# ipv4 print(fake.ipv6())#ip v6 print(fake.port_number())#端口號(hào)
可以這樣生成:
key_list = ["域名", "鏈接", "資源鏈接", "公司圖片鏈接", "ipv4", "ipv6","端口號(hào)"] for _ in range(200): info_list = [fake.domain_name(),fake.url(),fake.uri(),fake.image_url(),fake.ipv4(),fake.ipv6(),fake.port_number()] website_info = dict(zip(key_list,info_list)) print(website_info)
{'域名': 'xialiu.cn', '鏈接': 'https://www.jiamao.cn/', '資源鏈接': 'https://www.songhao.com/posts/list/search/about.asp', '公司圖片鏈接': 'https://placekitten.com/676/966', 'ipv4': '52.60.145.21', 'ipv6': '3458:a748:e9bb:17bc:a3f2:c9c0:9c63:16b9', '端口號(hào)': 62522} {'域名': 'peng.cn', '鏈接': 'https://60.cn/', '資源鏈接': 'http://fangzeng.cn/posts/category.html', '公司圖片鏈接': 'https://www.lorempixel.com/655/1002', 'ipv4': '4.255.156.194', 'ipv6': 'ec18:8efb:d080:e66e:552f:233b:8c25:166a', '端口號(hào)': 26634} {'域名': 'pingping.cn', '鏈接': 'https://www.juangao.cn/', '資源鏈接': 'https://www.22.com/list/privacy/', '公司圖片鏈接': 'https://placekitten.com/801/564', 'ipv4': '115.113.118.232', 'ipv6': 'd344:7490:96fd:35d0:adf2:807:e521:4606', '端口號(hào)': 54974} {'域名': 'mintang.org', '鏈接': 'https://91.cn/', '資源鏈接': 'https://www.dongguo.cn/tag/app/about/', '公司圖片鏈接': 'https://placeimg.com/448/92/any', 'ipv4': '132.188.93.127', 'ipv6': '3042:e325:a28f:5ab0:1fdb:8b33:6d5:99e8', '端口號(hào)': 15688} {'域名': 'lei.org', '鏈接': 'http://www.jiegang.cn/', '資源鏈接': 'https://www.gang.cn/app/post.htm', '公司圖片鏈接': 'https://www.lorempixel.com/125/956', 'ipv4': '89.10.171.82', 'ipv6': 'e786:ab37:5bca:47be:4298:17c6:3308:fb2e', '端口號(hào)': 61632} {'域名': 'zengfeng.cn', '鏈接': 'https://www.xiuyingkong.cn/', '資源鏈接': 'http://www.lin.cn/register.html', '公司圖片鏈接': 'https://placeimg.com/731/795/any', 'ipv4': '112.50.240.108', 'ipv6': '55fe:a08e:143e:2e04:bdd7:d19c:753c:7c99', '端口號(hào)': 5989} {'域名': 'duan.cn', '鏈接': 'http://pingyu.net/', '資源鏈接': 'http://daishen.cn/', '公司圖片鏈接': 'https://www.lorempixel.com/327/490', 'ipv4': '29.66.209.53', 'ipv6': 'b3ab:1b2c:df26:f517:66fa:f98a:813:5d58', '端口號(hào)': 54817} {'域名': 'wangfeng.cn', '鏈接': 'https://www.yangping.cn/', '資源鏈接': 'http://63.cn/', '公司圖片鏈接': 'https://placeimg.com/170/267/any', 'ipv4': '58.184.19.84', 'ipv6': '3a8:9879:36a9:8d74:de:59f6:50f0:fc2b', '端口號(hào)': 309} {'域名': 'taoqiang.cn', '鏈接': 'https://www.tao.cn/', '資源鏈接': 'https://71.net/home.htm', '公司圖片鏈接': 'https://placeimg.com/710/235/any', 'ipv4': '124.19.5.38', 'ipv6': 'ae55:cdff:34ab:18fd:a68:e88f:ad4:415', '端口號(hào)': 34035} {'域名': 'zheng.com', '鏈接': 'https://www.wantan.cn/', '資源鏈接': 'https://www.gong.cn/main/main.htm', '公司圖片鏈接': 'https://dummyimage.com/752x191', 'ipv4': '130.163.17.177', 'ipv6': 'fb5e:b866:2640:211e:29f2:c3c8:4505:f4f6', '端口號(hào)': 37949} {'域名': 'yan.cn', '鏈接': 'http://junxiuying.cn/', '資源鏈接': 'https://www.87.cn/terms/', '公司圖片鏈接': 'https://dummyimage.com/703x254', 'ipv4': '155.76.90.210', 'ipv6': 'bc18:a40b:55c7:ed9d:4d49:85dd:9ae:dbd0', '端口號(hào)': 20403} {'域名': 'qianglei.org', '鏈接': 'https://www.30.cn/', '資源鏈接': 'https://www.xiulanwei.cn/', '公司圖片鏈接': 'https://www.lorempixel.com/171/754', 'ipv4': '198.37.84.161', 'ipv6': '1d71:73e5:5bc7:fdeb:3123:4eff:6e64:8043', '端口號(hào)': 8369} {'域名': 'yaocao.cn', '鏈接': 'https://www.yanxiong.cn/', '資源鏈接': 'http://www.dingshen.cn/', '公司圖片鏈接': 'https://placeimg.com/831/866/any', 'ipv4': '26.81.152.72', 'ipv6': '7fa7:4d8a:ff88:ec82:7f99:d274:d562:7386', '端口號(hào)': 26475} {'域名': 'zhang.cn', '鏈接': 'https://jun.cn/', '資源鏈接': 'https://www.juan.cn/app/main/privacy.html', '公司圖片鏈接': 'https://www.lorempixel.com/460/267', 'ipv4': '92.181.175.8', 'ipv6': 'ccf7:19ab:2922:fbd8:dca5:b354:54a1:d505', '端口號(hào)': 19506} {'域名': 'minlei.cn', '鏈接': 'https://www.89.org/', '資源鏈接': 'https://www.shen.net/list/explore/login/', '公司圖片鏈接': 'https://placekitten.com/478/41', 'ipv4': '75.165.96.250', 'ipv6': '4124:405b:91fc:fe88:81c1:6e99:4d6c:d782', '端口號(hào)': 43605} {'域名': '44.cn', '鏈接': 'http://www.wm.cn/', '資源鏈接': 'https://www.91.cn/search/', '公司圖片鏈接': 'https://www.lorempixel.com/722/842', 'ipv4': '30.175.42.3', 'ipv6': 'c734:bb05:788c:31f6:19fa:a06f:c0a:5967', '端口號(hào)': 19840} {'域名': '99.cn', '鏈接': 'http://jiexie.cn/', '資源鏈接': 'http://yongtan.org/', '公司圖片鏈接': 'https://placeimg.com/606/260/any', 'ipv4': '50.205.173.81', 'ipv6': 'f521:ca9f:df5e:6f78:beeb:b4eb:eab9:221b', '端口號(hào)': 15932}
需求3
在爬蟲(chóng)過(guò)程中我們經(jīng)常需要更換ua,一個(gè)一個(gè)去網(wǎng)上找豈不是太麻煩了,現(xiàn)在可以使用這個(gè)庫(kù)直接生成,還可以挑選自己喜歡的瀏覽器:
for _ in range(20): print(fake.user_agent()) print(fake.chrome()) print(fake.firefox()) print(fake.internet_explorer()) print(fake.ios_platform_token()) print(fake.opera()) print(fake.safari())
結(jié)果如下:
Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; unm-US) AppleWebKit/534.3.3 (KHTML, like Gecko) Version/3.0.5 Mobile/8B115 Safari/6534.3.3 Mozilla/5.0 (Linux; Android 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/29.0.807.0 Safari/535.2 Mozilla/5.0 (X11; Linux i686; rv:1.9.5.20) Gecko/2020-05-30 04:42:27 Firefox/3.8 Mozilla/5.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Trident/3.0) iPad; CPU iPad OS 9_3_6 like Mac OS X Opera/8.99.(Windows 95; pa-IN) Presto/2.9.164 Version/10.00 Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/535.44.4 (KHTML, like Gecko) Version/5.0.5 Safari/535.44.4 Mozilla/5.0 (Linux; Android 7.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/47.0.877.0 Safari/535.2 Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.0 (KHTML, like Gecko) Chrome/24.0.891.0 Safari/535.0 Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_10_6; rv:1.9.4.20) Gecko/2019-07-27 11:43:09 Firefox/11.0 Mozilla/5.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90; Trident/4.1) iPad; CPU iPad OS 9_3_6 like Mac OS X Opera/9.63.(X11; Linux x86_64; nl-BE) Presto/2.9.182 Version/11.00
代碼
from faker import Faker fake = Faker(["zh_CN"]) Faker.seed(0) def get_person(): key_list = ["姓名","電話","郵箱","地址","工作單位","職位","城市","銀行卡號(hào)"] for _ in range(200): info_list = [fake.name(),fake.phone_number(),fake.email(),fake.address(),fake.company(),fake.job(),fake.city(),fake.credit_card_number()] person_info = dict(zip(key_list,info_list)) print(person_info) # print(fake.name()) #姓名 # print(fake.address()) # 地址 # print(fake.phone_number()) #電話 # print(fake.email()) #郵箱 # print(fake.company()) #公司 # print(fake.job()) # 職位 # print(fake.building_number()) #哪棟樓 # print(fake.city()) # 城市 # print(fake.user_name()) # print(fake.city_name()) # print(fake.credit_card_expire()) # print(fake.credit_card_number()) # print(fake.domain_name())#域名 # print(fake.image_url())#圖片鏈接 # print(fake.uri()) #資源 # print(fake.url()) #url # # print(fake.ipv4())# ipv4 # print(fake.ipv6())#ip v6 # print(fake.port_number())#端口號(hào) #print(fake.paragraph()) #print(fake.profile()) def get_website_info(): key_list = ["域名", "鏈接", "資源鏈接", "公司圖片鏈接", "ipv4", "ipv6","端口號(hào)"] for _ in range(200): info_list = [fake.domain_name(),fake.url(),fake.uri(),fake.image_url(),fake.ipv4(),fake.ipv6(),fake.port_number()] website_info = dict(zip(key_list,info_list)) print(website_info) def get_ua(): for _ in range(20): print(fake.user_agent()) print(fake.chrome()) print(fake.firefox()) print(fake.internet_explorer()) print(fake.ios_platform_token()) print(fake.opera()) print(fake.safari()) if __name__ == '__main__': #get_person() #get_website_info() get_ua()
以上就是python利用faker庫(kù)批量生成數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python faker庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python 如何用 Hypothesis 來(lái)自動(dòng)化單元測(cè)試
- Python基于Hypothesis測(cè)試庫(kù)生成測(cè)試數(shù)據(jù)
- python?playwright?庫(kù)上傳和下載操作(自動(dòng)化測(cè)試?playwright)
- Python測(cè)試框架pytest核心庫(kù)pluggy詳解
- python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解
- 基于python locust庫(kù)實(shí)現(xiàn)性能測(cè)試
- Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理
- python Hypothesis生成和執(zhí)行大量的測(cè)試用例
相關(guān)文章
python單例模式的應(yīng)用場(chǎng)景實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python單例模式的應(yīng)用場(chǎng)景實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02Python實(shí)現(xiàn)的批量修改文件后綴名操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)的批量修改文件后綴名操作,涉及Python目錄文件的遍歷、重命名等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12Python使用lambda表達(dá)式對(duì)字典排序操作示例
這篇文章主要介紹了Python使用lambda表達(dá)式對(duì)字典排序操作,結(jié)合實(shí)例形式分析了lambda表達(dá)式實(shí)現(xiàn)字典按鍵排序、按值排序、多條件排序相關(guān)操作技巧,需要的朋友可以參考下2019-07-07pycharm不在cmd中運(yùn)行卻在python控制臺(tái)運(yùn)行問(wèn)題解決
這篇文章主要介紹了pycharm不在cmd中運(yùn)行卻在python控制臺(tái)運(yùn)行問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了單鏈表、循環(huán)鏈表等的定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-09-09跟老齊學(xué)Python之編寫(xiě)類(lèi)之四再論繼承
本打算上篇文章就結(jié)束這個(gè)系列的,考慮了下,還是得加一章,算是對(duì)上一講的進(jìn)一步修改吧2014-10-10python3 deque 雙向隊(duì)列創(chuàng)建與使用方法分析
這篇文章主要介紹了python3 deque 雙向隊(duì)列創(chuàng)建與使用方法,結(jié)合實(shí)例形式分析了python3 deque 雙向隊(duì)列創(chuàng)建、添加、清空、拷貝等相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-03-03Python使用matplotlib創(chuàng)建Gif動(dòng)圖的思路
這篇文章主要介紹了Python使用matplotlib創(chuàng)建Gif動(dòng)圖,我們將討論matplotlib提供的名為“Animation”的動(dòng)畫(huà)庫(kù)之一,Python二維繪圖庫(kù)是Matplolib可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點(diǎn)圖等,需要的朋友可以參考下2022-04-04