项目场景:
批量查询手机号码信息
问题描述:
有同事需要批量查询手机号码的运营商,归属地区号信息
思路:
- 百度搜索,爬虫取数,学的不精暂时还不会直接放弃
- python 自带一个phone库,缺点是查不到虚拟号码不过也够用了
完整代码:
话不多说上代码
from phone import Phone
import xlrd
import os
import xlsxwriter
import tkinter.messagebox
from tkinter import *def get_phone_info(phone_num):phone_info = Phone().find(phone_num)if phone_info is None:phone = phone_numprovince ='Null' # 省city = 'Null' # 城市zip_code ='Null' # 邮编area_code = 'Null' # 区号phone_type = 'Null' # 运营商phone_count ='null'return phone, province, city, zip_code, area_code, phone_type,phone_countelse:try:phone = phone_info['phone']province = phone_info['province'] # 省city = phone_info['city'] # 城市zip_code = phone_info['zip_code'] # 邮编area_code = phone_info['area_code'] # 区号phone_type = phone_info['phone_type'] # 运营商if province==city:phone_count = province+phone_type # 汇总else:phone_count = province+city+phone_type # 汇总phone_info['phone_count']=phone_countexcept:print('----- the phone number is empty, please check your phone number! -----')print(phone_info)return phone, province, city, zip_code, area_code, phone_type, phone_countdef get_phone_info_excel(phone_number_path, sheet_num=0):# 解析地址file_name = os.path.splitext(phone_number_path)[0]file_realname = file_name.split('\\')[-1] # 文件名print(file_realname)exclename = file_name + '_get_info.xlsx'myWorkbook = xlsxwriter.Workbook(exclename) # 创建一个excel文件,用于待会写数据sheet_file_realname = myWorkbook.add_worksheet(file_realname) # 在文件中创建一个名为file_realname的sheet,不加名字默认为sheet1bold = myWorkbook.add_format({'bold': True}) # 设置一个加粗的格式对象# workfomat = workbook.add_format({# 'bold' : True, #字体加粗# 'border' : 1, #单元格边框宽度# 'align' : 'center', #对齐方式# 'valign' : 'vcenter', #字体对齐方式# 'fg_color' : '#F4B084', #单元格背景颜色# })sheet_file_realname.write(0, 0, u'电话号', bold) # 给第一行加标签sheet_file_realname.write(0, 1, u'省份', bold)sheet_file_realname.write(0, 2, u'城市', bold)sheet_file_realname.write(0, 3, u'邮编', bold)sheet_file_realname.write(0, 4, u'区号', bold)sheet_file_realname.write(0, 5, u'运营商', bold)sheet_file_realname.write(0, 6, u'汇总', bold)# 设置读Excel的编码格式xlrd.Book.encoding = "gbk"rb = xlrd.open_workbook(phone_number_path)sheet = rb.sheet_by_index(sheet_num) # 读第几个sheet,默认为第一个nrows = sheet.nrowsprint(nrows)for i in range(nrows):phone_num = sheet.cell_value(i, 0) # 假设电话号码在第一列,从第一行开始,也就是索引(0,0),if isinstance(phone_num,float):phone_num=str(int(phone_num))else:phone_num=phone_numif '-' in phone_num:phone_num1 =phone_num.split('-')[1]if len(phone_num1) >=7 and len(phone_num1) <=11:phone_num1 = int(phone_num1)# int是因为读入的时候自动变成float,会超过电话号码长度phone_info = get_phone_info(phone_num1)sheet_file_realname.write(i + 1, 0, phone_num)sheet_file_realname.write(i + 1, 1, phone_info[1])sheet_file_realname.write(i + 1, 2, phone_info[2])sheet_file_realname.write(i + 1, 3, phone_info[3])sheet_file_realname.write(i + 1, 4, phone_info[4])sheet_file_realname.write(i + 1, 5, phone_info[5])sheet_file_realname.write(i + 1, 6, phone_info[6])else:# int是因为读入的时候自动变成float,会超过电话号码长度sheet_file_realname.write(i + 1, 0, phone_num)sheet_file_realname.write(i + 1, 1, 'Null')sheet_file_realname.write(i + 1, 2, 'Null')sheet_file_realname.write(i + 1, 3, 'Null')sheet_file_realname.write(i + 1, 4, 'Null')sheet_file_realname.write(i + 1, 5, 'Null')sheet_file_realname.write(i + 1, 6, 'Null')else:phone_num1 = re.sub('[(),]','',phone_num)if len(phone_num1) >=7 and len(phone_num1) <=11:phone_num1 = int(phone_num1)# int是因为读入的时候自动变成float,会超过电话号码长度phone_info = get_phone_info(phone_num1)sheet_file_realname.write(i + 1, 0, phone_num)sheet_file_realname.write(i + 1, 1, phone_info[1])sheet_file_realname.write(i + 1, 2, phone_info[2])sheet_file_realname.write(i + 1, 3, phone_info[3])sheet_file_realname.write(i + 1, 4, phone_info[4])sheet_file_realname.write(i + 1, 5, phone_info[5])sheet_file_realname.write(i + 1, 6, phone_info[6] )else:# int是因为读入的时候自动变成float,会超过电话号码长度sheet_file_realname.write(i + 1, 0, phone_num)sheet_file_realname.write(i + 1, 1, 'Null')sheet_file_realname.write(i + 1, 2, 'Null')sheet_file_realname.write(i + 1, 3, 'Null')sheet_file_realname.write(i + 1, 4, 'Null')sheet_file_realname.write(i + 1, 5, 'Null')sheet_file_realname.write(i + 1, 6, 'Null')myWorkbook.close()print(os.path.realpath(exclename))tkinter.messagebox.showinfo('新生新的文件路径', os.path.realpath(exclename))if __name__ == "__main__":pwd = os.getcwd()phone_number_path = os.path.join(pwd,'电话号码.xlsx')print(phone_number_path)get_phone_info_excel(phone_number_path)
成果展现