Python实现的手机号归属地相关信息查询功能示例

yipeiwu_com5年前Python基础

本文实例讲述了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写程序,确实比C/C++之类方便许多。既然这个抽奖的数据不大,对效率要求并不高,所以采用python写,更加简洁、清晰、方便。 1.用到的模块 生成随机数的模...

PyQt5每天必学之创建窗口居中效果

PyQt5每天必学之创建窗口居中效果

本文实例为大家分享了PyQt5如何能够创建在桌面屏幕上居中窗口的具体代码,供大家参考,具体内容如下 下面的脚本说明我们如何能够创建在桌面屏幕上居中的窗口。 #!/usr/bin/py...

python里dict变成list实例方法

python里dict(字典)怎么变成list(列表)? 说明:列表不可以转换为字典 1、转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3}...

对python numpy数组中冒号的使用方法详解

对python numpy数组中冒号的使用方法详解

python中冒号实际上有两个意思:1.默认全部选择;2. 指定范围。 下面看例子 定义数组 X=array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[1...

Python多进程写入同一文件的方法

Python多进程写入同一文件的方法

最近用python的正则表达式处理了一些文本数据,需要把结果写到文件里面,但是由于文件比较大,所以运行起来花费的时间很长。但是打开任务管理器发现CPU只占用了25%,上网找了一下原因发现...