Example of the mobile phone number attribution-related information query function implemented by Python, python example
This example describes how to query information related to mobile phone number attribution in Python. We will share this with you for your reference. The details are as follows:
Query the location of the specified mobile phone number and other related information. Python implementation:
Mobile phone number file: test.txt
136932525521329662998913640810839157551066311511962273213904446048188747919531369565850013695658547159501790801557346277915217624651150184859891370652248213666519777136665151881885728752815575394501
Python implementation:
# Coding = UTF-8 # get provider information by phoneNumberfrom urllib import urlopenimport re # get html source code for urldef 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 informationdef parseString (src, result): pat = [] pat. append ('(? <= Geographic location: </span>). + (? = <Br/>) ') pat. append ('(? <= Card type: </span>). + (? = <Br/>) ') pat. append ('(? <= Carrier: </span>). + (? = <Br/>) ') pat. append ('(? <= Area code: </span>) \ d + (? = <Br/>) ') pat. append ('(? <= Zip code: </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 phoneNumdef 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 filedef 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)