#新建一個文本文件phoneNum.txt
#裏面每一行寫一個電話號碼
#寫完換行
#coding=utf-8
from phone import Phone
def getPhoneNum(file): #讀取源文件,獲取待查詢的手機號
try:
with open(file,"r") as f:
phonList = f.readlines() #讀取源手機號文檔中的手機號
#print(phonList)
return phonList #返回手機號列表。phonList
except: #兼容讀取文檔失敗
pass
def getPhoneInfo(phoneNum): #查詢函數
info = Phone().find(phoneNum) #通過phone庫查詢
try: #返回所有查詢的信息
phone = info['phone'] #手機號
province = info['province'] #歸屬地:省份
city = info['city'] #歸屬地,城市
zip_code = info['zip_code'] #郵政編碼
area_code = info['area_code'] #區域編碼
phone_type = info['phone_type'] #手機號運營商
print(phone+"\t"+province+city+"\t"+phone_type)
return ("\n"+phone+" \t"+province+city+" \t"+phone_type) #因爲我只需要手機號、區域、運營商,所以只返回這三個字段,其他字段,可以自己按需添加;
except: #兼容查詢失敗的情況
print("\n"+str(phoneNum.strip("\n"))+" \t"+"Error!")
return ("\n"+str(phoneNum.strip("\n"))+" \t"+"Error!")
if __name__ == "__main__":
listPhoneNum = getPhoneNum("phoneNum.txt") #通過getPhoneNum函數,讀取源文件。
listResult = []
for i in listPhoneNum:
try:
res = getPhoneInfo(i.strip("\n"))
listResult.append(res)
with open("result.txt","a") as f: #寫入結果文檔
f.write(res)
f.close()
except: #兼容出錯
res = "\n"+str(i).strip("\n") + "\t" + "Error!"
with open("result.txt","a") as f:
f.write(res)
f.close()
查詢手機號信息2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.