python利用faker庫批量生成測試數據
安裝
pip install faker
使用
簡單使用
本庫可生成姓名、地址、電話、郵箱、公司等等一系列數據。首先導入庫,實例化:
from faker import Fakerfake = Faker()
先看看正面生成一個人的姓名地址吧:
for _ in range(10): print(fake.name())
rs. Elizabeth Carter MDMark ObrienMadeline OliverRuth NewmanLori BennettVictor NolanBethany MitchellSteven HendersonShannon BurkeChristopher PachecoMorgan Hernandez
有時候我們并不想要英文數據,faker庫支持中文數據,只需要設置一下。
from faker import Fakerfake = 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()) # 城市
這時候小編需要幾萬個假人的個人信息數據,就可以這樣生成:
from faker import Fakerfake = Faker(['zh_CN'])Faker.seed(0)def get_person(): key_list = ['姓名','電話','郵箱','地址','工作單位','職位','城市','銀行卡號'] 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)
生成如下數據(只截取了部分):
{’姓名’: ’廖紅霞’, ’電話’: ’15087647593’, ’郵箱’: ’[email protected]’, ’地址’: ’澳門特別行政區麗華市崇文馬街F座 187784’, ’工作單位’: ’立信電子科技有限公司’, ’職位’: ’調研員’, ’城市’: ’大冶縣’, ’銀行卡號’: ’630453513933’}{’姓名’: ’柏秀梅’, ’電話’: ’18215871484’, ’郵箱’: ’[email protected]’, ’地址’: ’廣東省興城市花溪銀川路y座 722018’, ’工作單位’: ’數字100信息有限公司’, ’職位’: ’保險精算師’, ’城市’: ’張家港縣’, ’銀行卡號’: ’347515917953308’}{’姓名’: ’李龍’, ’電話’: ’18656012309’, ’郵箱’: ’[email protected]’, ’地址’: ’青海省沈陽縣龍潭合山路L座 508691’, ’工作單位’: ’濟南億次元網絡有限公司’, ’職位’: ’運輸經理/主管’, ’城市’: ’香港縣’, ’銀行卡號’: ’213145792302255’}{’姓名’: ’李桂香’, ’電話’: ’14507698456’, ’郵箱’: ’[email protected]’, ’地址’: ’上海市太原縣南長柳州路L座 661093’, ’工作單位’: ’思優網絡有限公司’, ’職位’: ’電腦操作員/打字員’, ’城市’: ’秀芳市’, ’銀行卡號’: ’4027142787890079’}{’姓名’: ’龍杰’, ’電話’: ’14563812066’, ’郵箱’: ’[email protected]’, ’地址’: ’新疆維吾爾自治區六盤水市普陀長沙街S座 185124’, ’工作單位’: ’創匯網絡有限公司’, ’職位’: ’飛行器設計與制造’, ’城市’: ’沈陽縣’, ’銀行卡號’: ’4977658236940223’}{’姓名’: ’楊坤’, ’電話’: ’13690042294’, ’郵箱’: ’[email protected]’, ’地址’: ’黑龍江省建軍市白云荊門路g座 704522’, ’工作單位’: ’通際名聯科技有限公司’, ’職位’: ’美容顧問’, ’城市’: ’濟南市’, ’銀行卡號’: ’2284779361534920’}{’姓名’: ’黃超’, ’電話’: ’13308731764’, ’郵箱’: ’[email protected]’, ’地址’: ’澳門特別行政區宇市南溪東莞街z座 638930’, ’工作單位’: ’信誠致遠網絡有限公司’, ’職位’: ’研發總監/部長/專家’, ’城市’: ’秀芳縣’, ’銀行卡號’: ’2244711801320403’}{’姓名’: ’徐麗麗’, ’電話’: ’14575868809’, ’郵箱’: ’[email protected]’, ’地址’: ’江西省雪梅縣新城王路p座 751744’, ’工作單位’: ’明騰科技有限公司’, ’職位’: ’機械機床’, ’城市’: ’馬鞍山市’, ’銀行卡號’: ’3527912560976700’}{’姓名’: ’石磊’, ’電話’: ’13125185367’, ’郵箱’: ’[email protected]’, ’地址’: ’青海省巖市朝陽李路b座 153751’, ’工作單位’: ’鑫博騰飛網絡有限公司’, ’職位’: ’網站運營專員’, ’城市’: ’桂蘭縣’, ’銀行卡號’: ’4886841269611610’}{’姓名’: ’金淑珍’, ’電話’: ’13476607541’, ’郵箱’: ’[email protected]’, ’地址’: ’內蒙古自治區淮安縣永川海門路h座 475420’, ’工作單位’: ’雨林木風計算機網絡有限公司’, ’職位’: ’數控編程’, ’城市’: ’晶市’, ’銀行卡號’: ’3594262235833243’}{’姓名’: ’田麗麗’, ’電話’: ’13261126486’, ’郵箱’: ’[email protected]’, ’地址’: ’山東省濤市魏都鄭州街X座 841892’, ’工作單位’: ’菊風公司網絡有限公司’, ’職位’: ’首席財務官 CFO’, ’城市’: ’雷縣’, ’銀行卡號’: ’4173407970341489262’}{’姓名’: ’馬紅梅’, ’電話’: ’18538640140’, ’郵箱’: ’[email protected]’, ’地址’: ’海南省長春市沙市深圳路d座 448298’, ’工作單位’: ’畢博誠信息有限公司’, ’職位’: ’漿紗工’, ’城市’: ’秀云市’, ’銀行卡號’: ’4211669728457637780’}{’姓名’: ’蔡濤’, ’電話’: ’15270930576’, ’郵箱’: ’[email protected]’, ’地址’: ’陜西省偉市懷柔蘇路N座 316756’, ’工作單位’: ’萬迅電腦信息有限公司’, ’職位’: ’銷售助理’, ’城市’: ’宇市’, ’銀行卡號’: ’4566763373906032’}{’姓名’: ’孔彬’, ’電話’: ’18523949184’, ’郵箱’: ’[email protected]’, ’地址’: ’海南省淑珍市蕭山昆明街s座 702163’, ’工作單位’: ’思優科技有限公司’, ’職位’: ’幕墻工程師’, ’城市’: ’平市’, ’銀行卡號’: ’6540803513853342’}{’姓名’: ’盧晨’, ’電話’: ’13347530481’, ’郵箱’: ’[email protected]’, ’地址’: ’江蘇省浩縣沙灣羅路X座 732455’, ’工作單位’: ’四通信息有限公司’, ’職位’: ’酒店前臺’, ’城市’: ’桂珍縣’, ’銀行卡號’: ’374301912633401’}{’姓名’: ’林麗麗’, ’電話’: ’13741953843’, ’郵箱’: ’[email protected]’, ’地址’: ’湖南省波市上街秦路j座 423793’, ’工作單位’: ’合聯電子傳媒有限公司’, ’職位’: ’加油站工作員’, ’城市’: ’鳳英縣’, ’銀行卡號’: ’2689577403827786’}{’姓名’: ’毛建華’, ’電話’: ’13235460305’, ’郵箱’: ’[email protected]’, ’地址’: ’江蘇省建軍市牧野上海路c座 351056’, ’工作單位’: ’創匯網絡有限公司’, ’職位’: ’叉車/鏟車工’, ’城市’: ’昆明縣’, ’銀行卡號’: ’213164887091665’}{’姓名’: ’胡玉珍’, ’電話’: ’14521532302’, ’郵箱’: ’[email protected]’, ’地址’: ’重慶市云縣秀英吳路z座 327385’, ’工作單位’: ’天開網絡有限公司’, ’職位’: ’牙科醫生’, ’城市’: ’成都縣’, ’銀行卡號’: ’6589840761657001’}{’姓名’: ’亓建華’, ’電話’: ’15543885643’, ’郵箱’: ’[email protected]’, ’地址’: ’新疆維吾爾自治區佛山縣花溪南寧街z座 162029’, ’工作單位’: ’創億科技有限公司’, ’職位’: ’情報信息分析人員’, ’城市’: ’慧縣’, ’銀行卡號’: ’4186428212917’}{’姓名’: ’曾萍’, ’電話’: ’15864440644’, ’郵箱’: ’[email protected]’, ’地址’: ’陜西省淮安縣白云鄭街P座 605823’, ’工作單位’: ’維旺明信息有限公司’, ’職位’: ’生產/營運’, ’城市’: ’成縣’, ’銀行卡號’: ’3573781327166449’}
還是可以的吧。
需求2
小編需要一些網站信息數據,先看一下屬性
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())#端口號
可以這樣生成:
key_list = ['域名', '鏈接', '資源鏈接', '公司圖片鏈接', 'ipv4', 'ipv6','端口號'] 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 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’, ’端口號’: 15932}
需求3
在爬蟲過程中我們經常需要更換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())
結果如下:
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.3Mozilla/5.0 (Linux; Android 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/29.0.807.0 Safari/535.2Mozilla/5.0 (X11; Linux i686; rv:1.9.5.20) Gecko/2020-05-30 04:42:27 Firefox/3.8Mozilla/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 XOpera/8.99.(Windows 95; pa-IN) Presto/2.9.164 Version/10.00Mozilla/5.0 (Windows; U; Windows NT 6.1) AppleWebKit/535.44.4 (KHTML, like Gecko) Version/5.0.5 Safari/535.44.4Mozilla/5.0 (Linux; Android 7.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/47.0.877.0 Safari/535.2Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.0 (KHTML, like Gecko) Chrome/24.0.891.0 Safari/535.0Mozilla/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.0Mozilla/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 XOpera/9.63.(X11; Linux x86_64; nl-BE) Presto/2.9.182 Version/11.00
代碼
from faker import Fakerfake = Faker(['zh_CN'])Faker.seed(0)def get_person(): key_list = ['姓名','電話','郵箱','地址','工作單位','職位','城市','銀行卡號'] 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())#端口號 #print(fake.paragraph()) #print(fake.profile())def get_website_info(): key_list = ['域名', '鏈接', '資源鏈接', '公司圖片鏈接', 'ipv4', 'ipv6','端口號'] 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庫批量生成數據的詳細內容,更多關于python faker庫的資料請關注好吧啦網其它相關文章!
相關文章:
