Python批量獲取并保存手機號歸屬地和運營商的示例
從Excel讀取一組手機號碼,批量查詢該手機號碼的運營商和歸屬地,并將其追加到該記錄的末尾。
import requestsimport jsonimport xlrdfrom xlutils.copy import copyhost = ’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(’rn’) new_workbook.save(new_file_path)if __name__ == ’__main__’: load_excel(file_path)
以上就是Python批量獲取并保存手機號歸屬地和運營商的示例的詳細內容,更多關于Python批量獲取并保存手機號的資料請關注好吧啦網其它相關文章!
相關文章:
