Python實現(xiàn)的手機號歸屬地相關(guān)信息查詢功能示例
本文實例講述了Python實現(xiàn)的手機號歸屬地相關(guān)信息查詢功能。分享給大家供大家參考,具體如下:
根據(jù)指定的手機號碼,查詢其歸屬地等相關(guān)信息,Python實現(xiàn):
手機號文件:test.txt
13693252552 13296629989 13640810839 15755106631 15119622732 13904446048 18874791953 13695658500 13695658547 15950179080 15573462779 15217624651 15018485989 13706522482 13666519777 13666515188 18857287528 15575394501
python實現(xiàn):
# 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('(?<=區(qū)號:</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程序設(shè)計有所幫助。
相關(guān)文章
徹底弄懂Python中的回調(diào)函數(shù)(callback)
回調(diào)函數(shù)就是一個通過函數(shù)指針調(diào)用的函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python中回調(diào)函數(shù)(callback)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python結(jié)合requests和Cheerio處理網(wǎng)頁內(nèi)容的操作步驟
Python因其簡潔明了的語法和強大的庫支持,成為了編寫爬蟲程序的首選語言之一,requests庫是Python中用于發(fā)送HTTP請求的第三方庫,而Cheerio庫則是一個用于解析HTML和XML文檔的庫,本文給大家介紹了Python結(jié)合requests和Cheerio處理網(wǎng)頁內(nèi)容的操作步驟2025-01-01
TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換
這篇文章主要介紹了TensorFlow2基本操作之 張量排序 填充與復(fù)制 查找與替換,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
python ImageDraw類實現(xiàn)幾何圖形的繪制與文字的繪制
這篇文章主要介紹了python ImageDraw類實現(xiàn)幾何圖形的繪制與文字的繪制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python數(shù)據(jù)可視化實現(xiàn)多種圖例代碼詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化實現(xiàn)多種圖例代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
詳解Python map函數(shù)及Python map()函數(shù)的用法
map() 會根據(jù)提供的函數(shù)對指定序列做映射。下面通過本文給大家介紹Python map函數(shù)及Python map()函數(shù)的用法,需要的朋友參考下吧2017-11-11

