早期都是經過Excel作數據統計,對某一列的數據插入處理。python
代碼功能是從A列純域名,將域名轉換爲IP,從域名A列獲得IP寫到B列。sql
#!/usr/bin/python #coding:utf-8 import sys import os import socket from xlutils.copy import copy import xlrd from urllib.parse import urlparse def domain2ip(domain): res = urlparse(domain) try : if res.netloc == "": ip = socket.gethostbyname(res.path) else: ip = socket.gethostbyname(res.netloc) except Exception as e: ip = "error" return ip def read_excel(filename, Row_num, Col_num): book = xlrd.open_workbook(filename) sheet = book.sheet_by_name('target') return sheet.cell_value(Row_num,Col_num) def excel_insert(excel_filename, Row_num, # 行 Col_num , # 列 Value # 內容 ): book = xlrd.open_workbook(excel_filename) # 打開excel new_book = copy(book) # 複製excel sheet = new_book.get_sheet(0) # 獲取第一個表格的數據 sheet.write(Row_num, Col_num, Value) # 修改1行2列的數據爲'Haha' new_book.save('secondsheet.xlsx') # 保存新的excel os.remove(excel_filename) # 刪除舊的excel os.rename('secondsheet.xlsx', excel_filename) # 將新excel重命名 def target_domain(filename): book = xlrd.open_workbook(filename) sheet = book.sheet_by_name('target') rows = sheet.nrows #獲取行數 cols = sheet.ncols #獲取列數 for rows_num in range(1,rows): domain = read_excel(filename, rows_num, 1) # 讀取第N行第1列的數據 print(rows_num,domain) ip = domain2ip(str(domain)) excel_insert(filename,rows_num,2,ip) # 寫進第N行第2列數據 if __name__ == '__main__': #Mysql_insert() # Mysql寫入 filename = "賞金列表.xlsx" target_domain(filename) # 目標列表的域名轉IP