python3 reqeusts后写入excel
生活随笔
收集整理的這篇文章主要介紹了
python3 reqeusts后写入excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用python通過手機號批量辨別運營商并寫入excel
初始文件:
具體代碼:
#coding=utf-8 import requests import re import xlrd import xlwtread_path = 'xxx.xlsx' #你的初始文件 write_path='xxx.xls' #你的生成文件# 設置手機號和運營商列表 phonelist = ['手機號'] yyslist = ['運營商'] # 讀取excel, def read_excel(read_path):# 打開文件filename = xlrd.open_workbook(read_path)# 獲取當前文檔的表(得到的是sheet的個數,一個整數)sheets=filename.nsheets# 通過sheet索引獲得sheet對象sheet = filename.sheet_by_index(0)# 獲取行數nrows = sheet.nrowsprint(nrows)# 獲取列數ncols = sheet.ncolsprint(ncols)# 獲取第2行,第2列數據(調取接口,獲取手機號的運營商信息,并存在yyslist)cell_value = sheet.cell_value(1, 1)print(type(cell_value))# print ("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+sheet.cell_value(1,0))# 獲取第2行以后的數據(第一列)#獲取每個手機號獲取信息,并用正則從返回中獲取catNamefor i in range(1, nrows):ur = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + str(int(sheet.cell_value(i, 0)))print(ur)response = requests.get(ur)response.enconding = 'utf-8'xinxi = response.textres = re.findall("catName:'(.*?)',", xinxi, re.M | re.S | re.U)print(res)yyslist.append(res)phonelist.append(int(sheet.cell_value(i, 0)))print(yyslist)#檢查運營商信息print(phonelist)#檢查手機號 #寫入新的excel def write_excel(write_path):# 創建工作簿workbook = xlwt.Workbook(encoding='utf-8')# 創建sheetdata_sheet = workbook.add_sheet('demo')# 生成第一列和第二列for i in range(len(yyslist)):data_sheet.write(i, 0, phonelist[i])data_sheet.write(i, 1, yyslist[i])# 保存文件# workbook.save('demo.xls')workbook.save(write_path)# book2 = copy(filename)# sheet = book2.get_sheet(0)# for i in range(1,9):# sheet.write(i, 1, yyslist[i-1])## book2.save('D:\saaa.xlsx') read_excel(read_path) write_excel(write_path)?
轉載請備注“文章轉載來自博客園-輸是誰”
?
轉載于:https://www.cnblogs.com/xinguichun/p/10993855.html
總結
以上是生活随笔為你收集整理的python3 reqeusts后写入excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教主的魔法
- 下一篇: classmethod和staticme