安全矩阵

 找回密码
 立即注册
搜索
查看: 3806|回复: 0

如何使用Python伪造一点也不假的假数据呢

[复制链接]

991

主题

1063

帖子

4315

积分

论坛元老

Rank: 8Rank: 8

积分
4315
发表于 2020-6-26 21:16:23 | 显示全部楼层 |阅读模式
原文链接:如何使用Python伪造一点也不假的假数据呢

工作中,有时候我们需要伪造一些假数据,如何使用 Python 伪造这些看起来一点也不假的假数据呢?
Python 有一个包叫 Faker,使用它可以轻易地伪造姓名、地址、手机号等等信息。
安装工具
  1. pip install faker
复制代码


创建Faker
安装完成后,使用时需要先创建一个 Faker 对象,创建方法有两种,一种是直接通过构造函数来创建,另一种是通过工厂函数来创建。
  1. >>> from faker import Faker, Factory
  2. >>> fake1 = Factory.create() # 通过工厂函数来创建
  3. >>> fake1.name() # 随机生成一个姓名
  4. 'Austin Parker'
  5. >>> fake2 = Faker() # 通过构造函数来创建
  6. >>> fake2.name() # 随机生成一个姓名
  7. 'Linda Castaneda'
复制代码
可以看到,创建了 faker 之后,可以调用 name() 方法来随机生成一个姓名。

本地化设置


上面生成的姓名都是英文姓名,如果想要生成中文姓名,该如何办呢?
Faker 支持创建时设置本地化,也就是指定区域。
  1. >>> fake = Faker("zh_CN")
  2. >>> fake.name()
  3. '西建平'
复制代码
可以看到,设置了本地化之后,可以随机生成中文姓名。

生成更多类型的数据

使用 Faker 除了可以生成姓名之外,还可以生成很多其他类型的数据。以下列举出一些常用的类型数据生成方式。
地址
  1. >>> fake.city() # 城市名称
  2. '辛集县'
  3. >>> fake.street_name() # 街道名称
  4. '荆街'
  5. >>> fake.country_code() # 国家编号
  6. 'DM'
  7. >>> fake.longitude() # 经度
  8. Decimal('134.520688')
  9. >>> fake.address() # 地址
  10. '吉林省宜都市清河俞街j座 292426'
  11. >>> fake.province() # 省份
  12. '宁夏回族自治区'
  13. >>> fake.latitude() # 纬度
  14. Decimal('-14.386640')
  15. >>> fake.street_address() # 街道地址
  16. '益路v座'
  17. >>> fake.city_suffix() # 市
  18. '市'
  19. >>> fake.postcode() # 邮政编码
  20. '530435'
  21. >>> fake.country() # 国家
  22. '维尔京群岛'
  23. >>> fake.street_suffix() # 街道后缀
  24. '街'
  25. >>> fake.district() # 区
  26. '安次'
  27. >>> fake.geo_coordinate(center=None, radius=0.001) # 地理坐标
  28. Decimal('52.985293')
  29. >>> fake.city_name() # 城市名称
  30. '沈阳'
  31. >>> fake.building_number() # 建筑编号
  32. 'C座'
复制代码

车牌号
  1. >>> fake.license_plate() # 车牌号
  2. '26FX4'
复制代码
银行
  1. >>> fake.bank_country()
  2. 'GB'
  3. >>> fake.iban()
  4. 'GB39SNOA2073712937476'
  5. >>> fake.bban()
  6. 'NYJX570813729289
复制代码
条形码



  1. >>> fake.ean8()  # 8位条码
  2. '63080728'
  3. >>> fake.ean13()  # 13位条码
  4. '0334204949323'
  5. >>> fake.ean(length=8)  # 自定义位数条码,只能选8或者13
  6. '81149919'
复制代码
颜色
  1. >>> fake.color_name() # 颜色名称
  2. 'SlateGray'
  3. >>> fake.safe_hex_color() # safe 颜色 16 进制编号
  4. '#111100'
  5. >>> fake.safe_color_name() # safe颜色名称
  6. 'black'
  7. >>> fake.rgb_color() # 颜色的 rgb 值
  8. '46,180,218'
  9. >>> fake.hex_color() # 颜色 16 进制编号
  10. '#81b632'
  11. >>> fake.rgb_css_color()
  12. 'rgb(27,224,190)'
复制代码

公司
  1. >>> fake.catch_phrase()
  2. 'Persistent bandwidth-monitored system engine'
  3. >>> fake.company_prefix() # 公司名前缀
  4. '联通时科'
  5. >>> fake.company() # 公司名
  6. '方正科技信息有限公司'
  7. >>> fake.company_suffix() # 公司名后缀
  8. '信息有限公司'
  9. >>> fake.bs()
  10. 'transition revolutionary action-items'
复制代码

信用卡
  1. >>> fake.credit_card_full(card_type=None) # 完整卡信息
  2. 'JCB 15 digit\n秀梅 段\n180053229428785 07/21\nCVC: 628\n'
  3. >>> fake.credit_card_provider(card_type=None) # 卡的提供者
  4. 'VISA 16 digit'
  5. >>> fake.credit_card_expire(start="now", end="+10y", date_format="%m/%y") # 卡的有效期
  6. '06/19'
  7. >>> fake.credit_card_number(card_type=None) # 卡号
  8. '4946562273912'
  9. >>> fake.credit_card_security_code(card_type=None) # 卡的安全密码
  10. '985'
复制代码
货币

  1. >>> fake.cryptocurrency()
  2. ('BTC', 'Bitcoin')
  3. >>> fake.cryptocurrency_code()
  4. 'USDT'
  5. >>> fake.currency_code()
  6. 'SZL'
  7. >>> fake.currency_name()
  8. 'Dominican peso'
  9. >>> fake.currency()
  10. ('ZWD', 'Zimbabwean dollar')
  11. >>> fake.cryptocurrency_name()
  12. 'Cardano'
复制代码
时间日期
  1. >>> fake.date_time(tzinfo=None) # 随机日期时间
  2. datetime.datetime(2001, 3, 18, 17, 57, 44)
  3. >>> fake.iso8601(tzinfo=None) # 以iso8601标准输出的日期
  4. '1973-11-16T22:58:37'
  5. >>> fake.date_time_this_month(before_now=True, after_now=False, tzinfo=None) # 本月的某个日期
  6. datetime.datetime(2017, 11, 1, 14, 33, 48)
  7. >>> fake.date_time_this_year(before_now=True, after_now=False, tzinfo=None) # 本年的某个日期
  8. datetime.datetime(2017, 3, 2, 13, 55, 31)
  9. >>> fake.date_time_this_decade(before_now=True, after_now=False, tzinfo=None)  # 本年代内的一个日期
  10. datetime.datetime(2010, 3, 26, 6, 33, 23)
  11. >>> fake.date_time_this_century(before_now=True, after_now=False, tzinfo=None)  # 本世纪一个日期
  12. datetime.datetime(2015, 7, 21, 19, 27, 53)
  13. >>> fake.date_time_between(start_date="-30y", end_date="now", tzinfo=None)  # 两个时间间的一个随机时间
  14. datetime.datetime(2005, 12, 3, 17, 17, 15)
  15. >>> fake.timezone() # 时区
  16. 'America/Guatemala'
  17. >>> fake.time(pattern="%H:%M:%S") # 时间(可自定义格式)
  18. '11:21:52'
  19. >>> fake.am_pm() # 随机上午下午
  20. 'PM'
  21. >>> fake.month() # 随机月份
  22. '02'
  23. >>> fake.month_name() # 随机月份名字
  24. 'August'
  25. >>> fake.year() # 随机年
  26. '1974'
  27. >>> fake.day_of_week() # 随机星期几
  28. 'Sunday'
  29. >>> fake.day_of_month() # 随机月中某一天
  30. '02'
  31. >>> fake.time_delta() # 随机时间延迟
  32. datetime.timedelta(13371, 27637)
  33. >>> fake.date_object()  # 随机日期对象
  34. datetime.date(1983, 1, 26)
  35. >>> fake.time_object() # 随机时间对象
  36. datetime.time(17, 8, 56)
  37. >>> fake.unix_time() # 随机unix时间(时间戳)
  38. 1223246848
  39. >>> fake.date(pattern="%Y-%m-%d") # 随机日期(可自定义格式)
  40. '1984-04-20'
  41. >>> fake.date_time_ad(tzinfo=None)  # 公元后随机日期
  42. datetime.datetime(341, 9, 11, 8, 6, 9)
复制代码
工作
  1. >>> fake.job()
  2. 'Development worker, community'
复制代码
文件
  1. >>> fake.unix_partition(prefix=None) # unix 分区
  2. '/dev/sdg5'
  3. >>> fake.file_name(category=None, extension=None) # 文件名
  4. '看到.flac'
  5. >>> fake.unix_device(prefix=None) # unix 设备
  6. '/dev/vdu'
  7. >>> fake.file_path(depth=1, category=None, extension=None)
  8. '/合作/国家.webm'
  9. >>> fake.file_extension(category=None) # 文件扩展信息
  10. 'mp3'
  11. >>> fake.mime_type(category=None)
  12. 'text/csv'
复制代码
互联网
  1. >>> fake.ipv4(network=False)  # ipv4地址
  2. '104.225.105.10'
  3. >>> fake.ipv6(network=False)  # ipv6地址
  4. 'dea6:ca11:39d0:b49f:fff1:82f1:bf88:698b'
  5. >>> fake.uri_path(deep=None) # uri路径
  6. 'search/categories'
  7. >>> fake.uri_extension() # uri扩展名
  8. '.htm'
  9. >>> fake.uri() # uri
  10. 'https://www.wei.com/terms/'
  11. >>> fake.url() # url
  12. 'http://zheng.org/'
  13. >>> fake.image_url(width=None, height=None)  # 图片url
  14. 'https://www.lorempixel.com/700/990'
  15. >>> fake.domain_word() # 域名主体
  16. 'hu'
  17. >>> fake.domain_name() # 域名
  18. 'hu.cn'
  19. >>> fake.tld() # 域名后缀
  20. 'com'
  21. >>> fake.user_name() # 用户名
  22. 'xia13'
  23. >>> fake.user_agent() # UA
  24. 'Opera/8.33.(Windows NT 5.1; an-ES) Presto/2.9.171 Version/10.00'
  25. >>> fake.mac_address() # MAC地址
  26. 'd6:38:cc:2a:76:b2'
  27. >>> fake.safe_email() # 安全邮箱
  28. 'mingli@example.net'
  29. >>> fake.free_email() # 免费邮箱
  30. 'tao44@gmail.com'
  31. >>> fake.company_email()  # 公司邮箱
  32. 'jingzhong@wang.cn'
  33. >>> fake.email() # 邮箱
  34. 'changjun@hao.com'
复制代码

电话号码
  1. >>> fake.phonenumber_prefix() # 运营商号段,手机号前三位
  2. 132
  3. >>> fake.msisdn()
  4. '5445934248280'
  5. >>> fake.phone_number() # 手机号
  6. '18666613199'
复制代码

身份证号码
  1. >>> fake.ssn(min_age=18, max_age=90)
  2. '460201193310128795'
  3. 人物
复制代码

人物
  1. >>> fake.suffix_female()
  2. ''
  3. >>> fake.last_name() # 姓
  4. '董'
  5. >>> fake.suffix_male()
  6. ''
  7. >>> fake.first_name_male() # 男性名
  8. '淑兰'
  9. >>> fake.name() # 姓名
  10. '空丹'
  11. >>> fake.first_name() # 名
  12. '凯'
  13. >>> fake.last_name_male() # 男性姓
  14. '却'
  15. >>> fake.name_male() # 女性姓名
  16. '申柳'
  17. >>> fake.romanized_name()
  18. 'Xia Xu'
  19. >>> fake.suffix()
  20. ''
  21. >>> fake.first_name_female()
  22. '琴'
  23. >>> fake.last_name_female()
  24. '羿'
  25. >>> fake.prefix_male()
  26. ''
  27. >>> fake.name_female()
  28. '明璐'
  29. >>> fake.prefix_female()
  30. ''
  31. >>> fake.first_romanized_name()
  32. 'Li'
  33. >>> fake.prefix()
  34. ''
  35. >>> fake.last_romanized_name()
  36. 'Tan'
复制代码

profile 人物属性



  1. >>> fake.profile(fields=None, sex=None)
  2. {'residence': '广东省呼和浩特县锡山龙街N座 403716', 'sex': 'M', 'website': ['http://zheng.org/'], 'birthdate': '1971-02-20', 'ssn': '513226197904080189', 'mail': 'jing95@hotmail.com', 'job': 'Ceramics designer', 'current_location': (Decimal('-86.424001'), Decimal('-153.969207')), 'blood_group': '0+', 'address': '广东省北京市永川深圳街w座 974761', 'company': '双敏电子传媒有限公司', 'username': 'xiajiang', 'name': '韩玉华'}
  3. >>> fake.simple_profile(sex=None)
  4. {'mail': 'dengna@yahoo.com', 'address': '西藏自治区楠市江北闻街v座 524952', 'username': 'guiying17', 'sex': 'F', 'birthdate': '1995-10-14', 'name': '姜敏'}
复制代码

lorem
  1. >>> fake.paragraphs(nb=3, ext_word_list=None)
  2. ['当然分析选择得到感觉关于.', '位置之间应用这种能够.', '你的处理上海.人员下载主要来自只是首页.图片有些所有详细发布.']
  3. >>> fake.word(ext_word_list=None)
  4. '最大'
  5. >>> fake.text(max_nb_chars=200, ext_word_list=None)
  6. '相关图片完成以及人民你的.出现语言计划浏览注意处理非常.\n一样制作个人留言留言这是说明.记者主要由于规定点击时候一个.公司时候系列推荐日期.\n汽车学校发现方法.合作学生她的查看各种次数所有或者.\n深圳世界文化不是结果一切.部分具有商品进行评论市场最后.数据回复名称谢谢系列.\n政府威望两个那些一个加入.以下那些需要以下.\n他 们不会工作资源那个这些所有文章.不会目前为什系统.'
  7. >>> fake.sentences(nb=3, ext_word_list=None)
  8. ['具有信息东西方式教育发布自己.', '业务类型社会作品方法.', '帖子作者都是.']
  9. >>> fake.paragraph(nb_sentences=3, variable_nb_sentences=True, ext_word_list=None)
  10. '不能加入最大重要.发现注意免费生产这是.'
  11. >>> fake.words(nb=3, ext_word_list=None)
  12. ['关于', '实现', '首页']
  13. >>> fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None)
  14. '发现成功一点系统空间全国比较.'
复制代码










回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-4-19 08:07 , Processed in 0.012786 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表