Python實現的手機號歸屬地相關資訊查詢功能示例

NO IMAGE

本文例項講述了Python實現的手機號歸屬地相關資訊查詢功能。分享給大家供大家參考,具體如下:

根據指定的手機號碼,查詢其歸屬地等相關資訊,Python實現:

手機號檔案:test.txt


13693252552
13296629989
13640810839
15755106631
15119622732
13904446048
18874791953
13695658500
13695658547
15950179080
15573462779
15217624651
15018485989
13706522482
13666519777
13666515188
18857287528
15575394501

python實現:


# coding=UTF-8
# get provider information by phoneNumber
from urllib import urlopen
import re
# get html source code for url
def getPageCode(url):
file = urlopen(url)
text = file.read()
file.close()
#  text = text.decode("utf-8")   # depending on coding of source code responded
return text
# parse html source code to get provider information
def parseString(src, result):
pat = []
pat.append('(?<=歸屬地:</span>). (?=<br />)')
pat.append('(?<=卡型別:</span>). (?=<br />)')
pat.append('(?<=運營商:</span>). (?=<br />)')
pat.append('(?<=區號:</span>)\d (?=<br />)')
pat.append('(?<=郵編:</span>)\d (?=<br />)')
item = []
for i in range(len(pat)):
m = re.search(pat[i], src)
if m:
v = m.group(0)
item.append(v)
return item
# get provider by phoneNum
def getProvider(phoneNum, result):
url = "http://www.sjgsd.com/n/?q=%s" %phoneNum
text = getPageCode(url)
item = parseString(text, result)
result.append((phoneNum, item))
# write result to file
def writeResult(result):
f = open("result.log", "w")
for num, item in result:
f.write("%s:\t" %num)
for i in item:
f.write("%s,\t" %i)
f.write("\n")
f.close()
if __name__ == "__main__":
result = []
for line in open("test.txt", "r"):
phoneNum = line.strip(" \t\r\n")
getProvider(phoneNum, result)
print("%s is finished" %phoneNum)
writeResult(result)

更多關於Python相關內容可檢視本站專題:《Python Socket程式設計技巧總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總

希望本文所述對大家Python程式設計有所幫助。

您可能感興趣的文章:

Python抓取手機號歸屬地資訊示例程式碼Python手機號碼歸屬地查詢程式碼python實現獲取Ip歸屬地等資訊Python使用淘寶API查詢IP歸屬地功能分享Python實現隨機生成有效手機號碼及身份證功能示例Python根據區號生成手機號碼的方法Python實現豎排列印傳單手機號碼易撕條python實現上傳樣本到virustotal並查詢掃描資訊的方法python使用線上API查詢IP對應的地理位置資訊例項