Python实现的手机号归属地相关信息查询功能示例
作者:JoeBlackzqq 时间:2021-02-05 23:10:05
本文实例讲述了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,手机号,归属地
0
投稿
猜你喜欢
浅谈Python3中print函数的换行
2023-12-15 18:24:31
ASP如何输出字符
2007-09-22 18:41:00
关于vue中的时间格式转化问题
2024-05-13 09:44:07
golang 执行命令行的实现
2024-04-28 09:14:40
详解如何在Apache中运行Python WSGI应用
2021-05-16 05:24:03
对python numpy数组中冒号的使用方法详解
2022-05-15 11:26:41
Python常用模块sys,os,time,random功能与用法实例分析
2023-07-16 12:50:57
python3.6数独问题的解决
2022-06-21 20:40:32
JS获取网页图片name属性的方法
2024-04-28 09:46:19
python 基本结构语句(函数和模块)
2023-06-14 00:37:56
Mysql最新版8.0.21下载安装配置教程详解
2024-01-21 23:58:17
asp去除html的函数代码分析附实例说明
2023-07-02 20:09:18
Python中defaultdict与dict的差异详情
2023-10-02 19:39:29
简单介绍一下pyinstaller打包以及安全性的实现
2021-07-23 15:34:56
SQL Server DBA日常检查常用SQL
2024-01-12 21:47:49
SQL Server 2008 R2 超详细安装图文教程
2024-01-24 16:41:12
js实现限定范围拖拽的示例
2024-04-29 13:38:55
费明红:什么样的404页面才是正确的?
2009-05-21 17:47:00
一文搞懂MySQL XA如何实现分布式事务
2024-01-22 23:25:19
解决django服务器重启端口被占用的问题
2023-11-10 14:14:12