前言
軟件測試工作中,經(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>