-2

Here m trying to extract a list of blacklist IP from a specific site and trying to make an excel sheet of the following fields : IP , date ...... New updated code is :

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()
Tejas
  • 1
  • 1
  • Have you ran the code? If so, what happens? Where and when does it error? What error messages do you get? What have you tried to fix the errors that appear? Do you get output? If it's the wrong output, what is the expected output? – Draken Apr 08 '16 at 06:16
  • Error is : Traceback (most recent call last): File "C:\Python27\blacklist.py", line 23, in sheet.write(row,col,w[x]) IndexError: list index out of range – Tejas Apr 08 '16 at 06:22
  • Have you tried `sheet.write(row,col,w[i])` instead? Not sure why you're creating an extra x value when you're using i to than iterate through the word – Draken Apr 08 '16 at 06:26
  • Error i get when I use w[i] : Traceback (most recent call last): File "C:\Python27\blacklist.py", line 23, in sheet.write(row,col,w[i]) File "C:\Python27\lib\site-packages\xlwt\Worksheet.py", line 1030, in write self.row(r).write(c, label, style) File "C:\Python27\lib\site-packages\xlwt\Row.py", line 240, in write StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label)) File "C:\Python27\lib\site-packages\xlwt\Row.py", line 159, in insert_cell raise Exception(msg) Exception: Attempt to overwrite cell: sheetname=u'Blacklist IP' rowx=1 colx=8 – Tejas Apr 08 '16 at 06:32
  • 1
    read [ask]. Stackoverflow is not a debugging service. – dandan78 Apr 08 '16 at 07:22

1 Answers1

0

I believe it should be this, though my python isn't great:

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')

Reasoning is that I believe you should be changing column every time you write a value, since that appears to be causing your current error. I also believe You should be changing row after each line, not just at the end of the code which then appears superfluous

[Edit update after user feedback]

Draken
  • 3,134
  • 13
  • 34
  • 54
  • yes u are right ,but still getting this Traceback (most recent call last): File "C:\Python27\blacklist.py",line 24, in sheet.write(row,col,w[i]) File "C:\Python27\lib\site-packages\xlwt\Worksheet.py", line 1030, in write self.row(r).write(c, label, style) File "C:\Python27\lib\site-packages\xlwt\Row.py", line 235, in write self.__adjust_bound_col_idx(col) File "C:\Python27\lib\site-packages\xlwt\Row.py", line 78, in __adjust_bound_col_idx raise ValueError("column index (%r) not an int in range(256)" % arg) ValueError: column index (256) not an int in range(256) – Tejas Apr 08 '16 at 06:54
  • That's because your split string is longer than 256 characters and I'm guessing xlwt can only handle 256 columns, would it cause an issue to move on to another row if you hit that limit? – Draken Apr 08 '16 at 06:56
  • If you don't mind another row, check the updated code. Though could cause an issue distinguishing if it moved a row due to a new line or because you hit the 256 column limit – Draken Apr 08 '16 at 06:58
  • More details on xlwt limitations: http://stackoverflow.com/questions/7658513/python-xlwt-more-than-256-columns – Draken Apr 08 '16 at 07:01
  • there will be only 5 columns , please check the link https://www.dshield.org/ipsascii.html?limit=100 for the better view – Tejas Apr 08 '16 at 07:06
  • can someone confirm on the syntax of this line in code : sheet.write(row,col,w[i]) .... I believe that due to some error with w[i] – Tejas Apr 08 '16 at 07:16
  • I'm guessing this is due to whitespace, updating – Draken Apr 08 '16 at 07:18
  • I've added extra debugging, it might point you into the direction of the error. If it doesn't, can you paste the output into the question here? – Draken Apr 08 '16 at 07:35