python手机号前7位归属地爬虫代码实例
作者:wanli001 时间:2021-01-23 05:20:34
需求分析
项目上需要用到手机号前7位,判断号码是否合法,还有归属地查询。旧的数据是几年前了太久了,打算用python爬虫重新爬一份
单线程版本
# coding:utf-8
import requests
from datetime import datetime
class PhoneInfoSpider:
def __init__(self, phoneSections):
self.phoneSections = phoneSections
def phoneInfoHandler(self, textData):
text = textData.splitlines(True)
# print("text length:" + str(len(text)))
if len(text) >= 9:
number = text[1].split('\'')[1]
province = text[2].split('\'')[1]
mobile_area = text[3].split('\'')[1]
postcode = text[5].split('\'')[1]
line = "number:" + number + ",province:" + province + ",mobile_area:" + mobile_area + ",postcode:" + postcode
line_text = number + "," + province + "," + mobile_area + "," + postcode
print(line_text)
# print("province:" + province)
try:
f = open('./result.txt', 'a')
f.write(str(line_text) + '\n')
except Exception as e:
print(Exception, ":", e)
def requestPhoneInfo(self, phoneNum):
try:
url = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=' + phoneNum
response = requests.get(url)
self.phoneInfoHandler(response.text)
except Exception as e:
print(Exception, ":", e)
def requestAllSections(self):
# last用于接上次异常退出前的号码
last = 0
# last = 4
# 自动生成手机号码,后四位补0
for head in self.phoneSections:
head_begin = datetime.now()
print(head + " begin time:" + str(head_begin))
# for i in range(last, 10000):
for i in range(last, 10):
middle = str(i).zfill(4)
phoneNum = head + middle + "0000"
self.requestPhoneInfo(phoneNum)
last = 0
head_end = datetime.now()
print(head + " end time:" + str(head_end))
if __name__ == '__main__':
task_begin = datetime.now()
print("phone check begin time:" + str(task_begin))
# 电信,联通,移动,虚拟运营商
dx = ['133', '149', '153', '173', '177', '180', '181', '189', '199']
lt = ['130', '131', '132', '145', '146', '155', '156', '166', '171', '175', '176', '185', '186', '166']
yd = ['134', '135', '136', '137', '138', '139', '147', '148', '150', '151', '152', '157', '158', '159', '172',
'178', '182', '183', '184', '187', '188', '198']
add = ['170']
all_num = dx + lt + yd + add
# print(all_num)
print(len(all_num))
# 要爬的号码段
spider = PhoneInfoSpider(all_num)
spider.requestAllSections()
task_end = datetime.now()
print("phone check end time:" + str(task_end))
发现爬取一个号段,共10000次查询,单线程版大概要多1个半小时,太慢了。
多线程版本
# coding:utf-8
import requests
from datetime import datetime
import queue
import threading
threadNum = 32
class MyThread(threading.Thread):
def __init__(self, func):
threading.Thread.__init__(self)
self.func = func
def run(self):
self.func()
def requestPhoneInfo():
global lock
while True:
lock.acquire()
if q.qsize() != 0:
print("queue size:" + str(q.qsize()))
p = q.get() # 获得任务
lock.release()
middle = str(9999 - q.qsize()).zfill(4)
phoneNum = phone_head + middle + "0000"
print("phoneNum:" + phoneNum)
try:
url = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=' + phoneNum
# print(url)
response = requests.get(url)
# print(response.text)
phoneInfoHandler(response.text)
except Exception as e:
print(Exception, ":", e)
else:
lock.release()
break
def phoneInfoHandler(textData):
text = textData.splitlines(True)
if len(text) >= 9:
number = text[1].split('\'')[1]
province = text[2].split('\'')[1]
mobile_area = text[3].split('\'')[1]
postcode = text[5].split('\'')[1]
line = "number:" + number + ",province:" + province + ",mobile_area:" + mobile_area + ",postcode:" + postcode
line_text = number + "," + province + "," + mobile_area + "," + postcode
print(line_text)
# print("province:" + province)
try:
f = open('./result.txt', 'a')
f.write(str(line_text) + '\n')
except Exception as e:
print(Exception, ":", e)
if __name__ == '__main__':
task_begin = datetime.now()
print("phone check begin time:" + str(task_begin))
dx = ['133', '149', '153', '173', '177', '180', '181', '189', '199']
lt = ['130', '131', '132', '145', '155', '156', '166', '171', '175', '176', '185', '186', '166']
yd = ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '172', '178',
'182', '183', '184', '187', '188', '198']
all_num = dx + lt + yd
print(len(all_num))
for head in all_num:
head_begin = datetime.now()
print(head + " begin time:" + str(head_begin))
q = queue.Queue()
threads = []
lock = threading.Lock()
for p in range(10000):
q.put(p + 1)
print(q.qsize())
for i in range(threadNum):
middle = str(i).zfill(4)
global phone_head
phone_head = head
thread = MyThread(requestPhoneInfo)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
head_end = datetime.now()
print(head + " end time:" + str(head_end))
task_end = datetime.now()
print("phone check end time:" + str(task_end))
多线程版的1个号码段1000条数据,大概2,3min就好,cpu使用飙升,大概维持在70%左右。
总共40多个号段,爬完大概1,2个小时,总数据41w左右
来源:https://www.cnblogs.com/wanli002/p/11413281.html
标签:python,手机,归属地,爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何做一个密码“生成器”?
2010-07-12 18:51:00
ASP 获取腾讯IP地址的代码
2011-02-26 11:19:00
python获取命令行输入参数列表的实例代码
2022-02-26 15:48:41
Pycharm使用爬虫时遇到etree红线问题及解决
2021-09-04 19:07:06
![](https://img.aspxhome.com/file/2023/8/112808_0s.png)
asp金额大小写转换完全无错版
2007-09-26 09:38:00
Python如何生成随机高斯模糊图片详解
2021-08-25 13:07:18
![](https://img.aspxhome.com/file/2023/1/107061_0s.png)
python 监控服务器是否有人远程登录(详细思路+代码)
2022-05-16 04:08:26
![](https://img.aspxhome.com/file/2023/8/109628_0s.png)
SQL Server数据库对上亿表的操作
2008-11-16 18:13:00
Bad Tags — html有害的标签
2008-10-13 19:47:00
Python实现单例模式的四种方式详解
2023-07-12 03:07:43
![](https://img.aspxhome.com/file/2023/5/97345_0s.png)
破解加密的网页代码方法
2010-03-16 12:35:00
python-try-except:pass的用法及说明
2022-07-21 13:22:24
python各种语言间时间的转化实现代码
2022-06-27 14:54:28
用正则替换所有URL
2009-03-13 13:51:00
Python使用read_csv读数据遇到分隔符问题的2种解决方式
2022-01-13 13:30:47
![](https://img.aspxhome.com/file/2023/7/103507_0s.png)
python 中yaml文件用法大全
2021-11-30 08:02:29
![](https://img.aspxhome.com/file/2023/4/65964_0s.png)
Python详细讲解图像处理的而两种库OpenCV和Pillow
2022-08-14 05:23:19
![](https://img.aspxhome.com/file/2023/3/79493_0s.png)
oracle的nvl函数的使用介绍
2023-07-19 14:42:20
趣味Python实战练习之自动更换桌面壁纸脚本附源码
2021-11-03 09:12:33
![](https://img.aspxhome.com/file/2023/9/82549_0s.jpg)
Python HTML解析器BeautifulSoup用法实例详解【爬虫解析器】
2023-05-21 01:32:42