03-0006 Python批量查询手机归属地

1.源码

1.1 文件切割以及数据查询部分

import threading
import urllib
import urllib.request

# 线程结束的标志
exitFlag = 0
#线程个数
thread_num=0

# 文件分割
# 每一百个手机号,放进一个文件里面,依次命名
# 将其放进file_cut文件夹中
# 这里使用的是相对路径,需要自己新建对应的文件夹
f = open("data.txt")             # 返回一个文件对象
fo=open("file_cut/file1.txt","w")
line = f.readline()             # 调用文件的 readline()方法
count=1
while line:
    print(line)                 # 后面跟 ',' 将忽略换行符
    fo.write(line)
    line = f.readline()
    if count%100==0:
        fo.close()
        path='file_cut/file'+str(int(count/100)+1)+'.txt'
        fo = open(path, "w")
    count+=1
fo.close()
f.close()

# 获取线程数
# 线程的个数等于文件的个数
thread_num=int(count/100)+1


# 多线程处理
class myThread(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        print("Starting " + self.name)
        phone_ser(self.threadID)
        print("Exiting " + self.name)
# 用于查询归属地,将返回内容写入对应的文件之中,根据线程的编号
def phone_ser(threadID):
    file_path='file_cut/file'+str(threadID+1)+'.txt'
    file_path2='file_com/file'+str(threadID+1)+'.txt'
    file = open(file_path)
    file2 = open(file_path2, "w")
    line = file.readline()
    while line:
        url = 'http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=' + line
        req = urllib.request.Request(url)
        res = urllib.request.urlopen(req)
        html = res.read().decode('utf-8')
        # print(html)
        file2.write(html)
        file2.write("\n")
        line = file.readline()
    file.close()
    file2.close()

thread=[]
# 循环创建线程
for i in range(0,thread_num):
    thread_name="Thread"+str(i)
    temp_thread=myThread(i,thread_name,1)
    thread.append(temp_thread)

# 循环开启线程
for i in range(0,thread_num):
    thread[i].start()

1.2 合并部分

# 数据整理
import re
file_path2 = 'file_com.txt'
file2 = open(file_path2, "w")
for i in range(0,113):
    file_path = 'file_com/file' + str(i + 1) + '.txt'
    file = open(file_path)
    line=file.readline()
    while line:
        pattern = re.compile(r'\"(.*?)\"')
        str_list = pattern.findall(line)
        new_line=str_list[1]+" "+str_list[5]+" "+str_list[7]+" "+str_list[11]+"\n"
        file2.write(new_line)
        line=file.readline()
    file.close()

file2.close()
# 这一部分的备注比较少,因为在python里面,文件操作确实很简单。

2.结果

file_com存储需要合并的文件【必须要有】
file_cut存储切割之后的文件【必须要有】
data.txt是原始数据,可以直接从excel中进行复制粘贴【必须要有】
file_com.txt是最终要得到的文件【自动生成,可以覆盖】
file_com.py数据合并部分的代码
phonenum.py数据切割及查询部分的代码

在这里插入图片描述

file_com文件夹

在这里插入图片描述

file_cut文件夹

在这里插入图片描述

文件名都一样但是大小不一样,当运行过代码之后一定要生成对应数量的文件。
运行的时候在编译器里面会有[finished]的标志,但是直接用python.exe文件运行是不会有这些的,会自己退出,如果中途卡住,等了好久也不退出,说明该程序可能已经被检测为了爬虫,需要重新启动。
程序尚且有不足之处,没有对于线程卡住的情况进行处理,可以对于每个线程添加时间限制,到时间之后自动结束,然后自动重启,直到该线程结束。
但是上面的机制,还不会写,所以就没有写。

发布了14 篇原创文章 · 获赞 9 · 访问量 4291

猜你喜欢

转载自blog.csdn.net/qq_37766828/article/details/91410102
今日推荐