帝王谷资源网 Design By www.wdxyy.com

从Excel读取一组手机号码,批量查询该手机号码的运营商和归属地,并将其追加到该记录的末尾。

import requests
import json
import xlrd
from xlutils.copy import copy

host = 'https://cx.shouji.360.cn/phonearea.php'
# excel文件路径
file_path = "F:\\temp.xlsx"
# 新文件路径
new_file_path = "F:\\temp(含归属地+运营商).xlsx"


def query(phone_no):
  resp = requests.get(host, {'number': phone_no}).content.decode('utf-8')
  js = json.loads(resp)
  print(js)
  return js['data']


def load_excel(path):
  # 打开文件
  data = xlrd.open_workbook(path)

  # 打开第一个sheet
  table = data.sheet_by_index(0)

  new_workbook = copy(data)
  new_worksheet = new_workbook.get_sheet(0)

  rows = table.nrows
  cols = table.ncols
  print("总行数:" + str(rows))
  print("总列数:" + str(cols))

  for row in range(rows):
    print("row --> " + str(row + 1))
    for col in range(cols):
      cel_val = table.cell(row, col).value
      print(cel_val)
      new_worksheet.write(row, col, cel_val)
    if row > 0:
      # 手机号,在第一行之后的第二列
      phone_no = table.cell(row, 1).value
      js = query(phone_no)
      new_worksheet.write(row, cols + 1, js['province'] + js['city'])
      new_worksheet.write(row, cols + 2, js['sp'])
    else:
      new_worksheet.write(row, cols + 1, "归属地")
      new_worksheet.write(row, cols + 2, "运营商")
    print('\r\n')
  new_workbook.save(new_file_path)


if __name__ == '__main__':
  load_excel(file_path)

以上就是Python批量获取并保存手机号归属地和运营商的示例的详细内容,更多关于Python批量获取并保存手机号的资料请关注其它相关文章!

标签:
python,获取手机号,python,手机号归属地,python,手机号运营商

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com