Python implementation of the mobile phone number home information query example

  • 2020-06-03 07:08:44
  • OfStack

An example of Python is presented in this paper. To share for your reference, specific as follows:

According to the designated mobile phone number, the user can query the location of the user and other relevant information. Python implements:

Mobile phone number file: ES7en.txt


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

python implementation:


# 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('(?<= Belong to: </span>).+(?=<br />)')
  pat.append('(?<= Card type: </span>).+(?=<br />)')
  pat.append('(?<= Operator: </span>).+(?=<br />)')
  pat.append('(?<= The 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 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)

For more information about Python, please visit Python Socket Programming Skills summary, Python Data Structure and Algorithm Tutorial, Python Function Usage Skills Summary, Python String Manipulation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Manipulation Skills Summary.

I hope this article has been helpful in Python programming.


Related articles: