Home > Backend Development > Python Tutorial > Python implements number ownership query function

Python implements number ownership query function

巴扎黑
Release: 2017-08-09 10:57:38
Original
3037 people have browsed it

This article mainly introduces the information query function related to the location of mobile phone numbers implemented in Python, involving Python file reading and related operation skills based on third-party interface calls to query information. Friends in need can refer to the following

The example of this article describes the information query function related to the location of mobile phone numbers implemented in Python. Share it with everyone for your reference, the details are as follows:

According to the specified mobile phone number, query its location and other related information, Python implementation:

Mobile phone number file: test.txt


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

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(&#39;(?<=归属地:</span>).+(?=<br />)&#39;)
  pat.append(&#39;(?<=卡类型:</span>).+(?=<br />)&#39;)
  pat.append(&#39;(?<=运营商:</span>).+(?=<br />)&#39;)
  pat.append(&#39;(?<=区号:</span>)\d+(?=<br />)&#39;)
  pat.append(&#39;(?<=邮编:</span>)\d+(?=<br />)&#39;)
  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)
Copy after login

The above is the detailed content of Python implements number ownership query function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template