領(lǐng)導(dǎo)要我6點下班前創(chuàng)建1000個有效的手機號,現(xiàn)在5點半了!教你用random模塊10分鐘搞定!

前言

軟件測試工作中,經(jīng)常需要用到很多手機號進行注冊或其它操作,如何生成有效有手機號呢?我們可以使用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個尾數(shù)
 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):
?
# 封裝公共的數(shù)據(jù)
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>

運行結(jié)果

<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":"聯(lián)通","card":""},"error_code":0}
1</pre>

我們經(jīng)常是使用免費的API去檢測手機號的正確性,在想有沒有更簡單的方式來檢驗?zāi)兀慨?dāng)然是有的,有一個第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)
?
# 獲取手機號相關(guān)歸屬信息
phone_info = phone.Phone().find(n_phone)
print(phone_info)
1234567891011121314151617</pre>

運行結(jié)果

<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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容