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,手机号,归属地
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
form 元素内的字段 name 不要跟 form 属性名称一致
2008-10-22 13:25:00
10个美观实用的 jQuery/Mootools 日历插件
2009-09-17 13:03:00
![](https://img.aspxhome.com/file/UploadPic/20099/17/2304116tf-27s.gif)
ORCAL 临时创建表与删除表
2023-07-12 19:30:05
CSS中写expression可能会在Chrome中有问题
2010-01-29 13:10:00
![](https://img.aspxhome.com/file/UploadPic/20101/29/k2010125224713-48s.gif)
pycharm 配置远程解释器的方法
2022-05-22 05:10:40
![](https://img.aspxhome.com/file/2023/9/115529_0s.jpg)
Django搭建项目实战与避坑细节详解
2021-04-30 06:46:46
![](https://img.aspxhome.com/file/2023/2/78482_0s.png)
python pandas dataframe 去重函数的具体使用
2023-10-15 00:56:36
Pandas实现Dataframe的重排和旋转
2023-11-03 23:47:23
![](https://img.aspxhome.com/file/2023/8/87878_0s.png)
在Python中字符串、列表、元组、字典之间的相互转换
2021-08-30 07:09:42
![](https://img.aspxhome.com/file/2023/7/65497_0s.png)
浅谈pandas中shift和diff函数关系
2022-11-15 08:48:42
python 处理微信对账单数据的实例代码
2023-12-19 21:45:23
Python tkinter制作单机五子棋游戏
2021-08-01 03:31:43
![](https://img.aspxhome.com/file/2023/8/65878_0s.jpg)
kafka-python批量发送数据的实例
2023-04-24 04:40:23
php在linux环境中如何使用redis详解
2023-06-14 23:38:30
Python Flask-web表单使用详解
2022-07-14 01:55:59
![](https://img.aspxhome.com/file/2023/7/90747_0s.jpg)
Python 使用双重循环打印图形菱形操作
2021-07-16 14:13:47
![](https://img.aspxhome.com/file/2023/4/120874_0s.jpg)
php面象对象数据库操作类实例
2023-10-13 22:33:05
asp去除html的函数代码分析附实例说明
2023-07-02 20:09:18
CentOS7安装Python3的教程详解
2023-10-17 02:40:28
ASP编程入门进阶(十):Global.asa文件
2008-07-12 07:05:00