欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

我的领导要我在 6:00 值班结束前创建 1000 个有效手机号码,而现在是 5:30!教你如何使用随机模块在 10 分钟内完成!

最编程 2024-04-08 13:47:11
...

前言

软件测试工作中,经常需要用到很多手机号进行注册或其它操作,如何生成有效有手机号呢?我们可以使用random模块生成手机号

python脚本生成随机手机号码

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n582" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#! /usr/bin/python3
​
#@Time    : 2020/8/8 10:40
#@Author  :  公众号测试员小何
​
import random
​
def greatphone():
 """生成随机手机号"""
​
 # choice方法随机抽取列表中的手机号码段
 str_start=random.choice(['135','136','138', "186"])
 # sample方法,列表中随机抽取8个尾数
 str_end = ''.join(random.sample('0123456789', 8))
 phone = str_start + str_end
 # 返回的手机号用于传给API检验
 return phone
1234567891011121314151617</pre>

通过 API接口获取手机号码归属地

接口地址:http://apis.juhe.cn/mobile/get

返回格式:json/xml

请求方式:get

请求示例:http://apis.juhe.cn/mobile/get?phone=13429667914&key=您申请的KEY

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n588" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#! /usr/bin/python3
​
# @Time    : 2020/8/8 17:03
# @Author  : 公众号测试员小何
​
import unittest
import requests
import json
from day2 import GreatPhone
​
class Test_Moblie(unittest.TestCase):
​
# 封装公共的数据
def common(self, phone):
 url = "http://apis.juhe.cn/mobile/get"
 date = {
 'key': "4391b7dd8213662798c3ac3da9f54ca8",
 'phone': phone
 }
 self.response = requests.get(url, params=date)
 return self.response
​
 def test_1(self):
 self.common(GreatPhone.phone())
 print(self.response.text)
​
if __name__ == '__main__':
 unittest.main()
12345678910111213141516171819202122232425262728</pre>

运行结果

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n590" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{"resultcode":"200","reason":"Return Successd!","result":{"province":"广东","city":"东莞","areacode":"0769","zip":"523000","company":"联通","card":""},"error_code":0}
1</pre>

我们经常是使用免费的API去检测手机号的正确性,在想有没有更简单的方式来检验呢?当然是有的,有一个第3方的模块:phone可以简单的处理这个问题

通过 phone模块获取手机号码归属地

安装模块

pip install phone

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n595" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#! /usr/bin/python3
# -*- coding:utf-8 -*-'
​
# @FileName: day4.py
# @Time    : 2020/8/8 20:47
# @Author  : 公众号测试员小何
​
import phone
from day2 import GreatPhone
​
# 获取手机号
n_phone = GreatPhone.phone()
print(n_phone)
​
# 获取手机号相关归属信息
phone_info = phone.Phone().find(n_phone)
print(phone_info)
1234567891011121314151617</pre>

运行结果

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n597" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">{'phone': '13534619807', 'province': '广东', 'city': '潮州', 'zip_code': '521000', 'area_code': '0768</pre>