这里我试图从特定网站中提取黑名单IP列表,并尝试制作以下字段的Excel表格: IP,日期......新的更新代码是:
import xlwt
import urllib
def Bl():
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python')
sheet.write(0,0,'Blacklist IP')
sheet.write(0,1,'Reports')
sheet.write(0,2,'abcd')
sheet.write(0,3,'date')
sheet.write(0,4,'Date')
row = 1 #counter for rows.
col = 0 #counter for columns.
x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
if not li.startswith("#"):
sheet.write(row,col,w[0])
sheet.write(row,1,w[1])
sheet.write(row,2,w[2])
sheet.write(row,3,w[3])
sheet.write(row,4,w[4])
row = row + 1
wbk.save('new.xls')
Bl()
答案 0 :(得分:0)
我相信它应该是这样,虽然我的python并不是很棒:
import xlwt
import urllib
link = 'https://www.dshield.org/ipsascii.html?limit=100'
p = urllib.urlopen(link)
text = p.readlines()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Blacklist IP')
row = 1 #counter for rows.
col = 0 #counter for columns.
#x = 0 #counter for printing the n'th element in the string w.
for line in text:
li = line.strip()
w = line.split()
print "The line " + line + " was split into " + len(w) + " size"
for i in range(0,len(w)-1):
if not li.startswith("#") and not li.isspace():
sheet.write(row,col,w[i])
#x = x + 1
col = col + 1
if col > 256:
print "Line of " + li
print "Row: " + row
print "Col: " + col
raise ValueError('Too many columns, can\'t insert ' + w)
row = row + 1
wbk.save('new.xls')
推理是我相信你每次写一个值时都应该更改列,因为这似乎是导致你当前的错误。我也相信你应该在每一行之后改变行,而不仅仅是在代码的末尾,这看起来是多余的
[在用户反馈后编辑更新]