Hello everyone. I was wondering if somebody could solve this problem for me. I have a bot that is required to refresh and grab back times here is the code for that task :

def findNST(html):
    NST = re.findall(r'<td id="nst">(.*) am', html)
    if NST == []:
        NST = re.findall(r'<td id="nst">(.*) pm', html)
    p = re.compile('\d+')
    Times = p.findall(NST[0])
    return NST, Times

Im having an issue where its crashing becuase sometimes the site goes down for a sec or it gets a blank page and it doesnt grab the time this is the error I get :

Traceback (most recent call last):
  File "C:\Python27\Bot folder\frankie\client.py", line 170, in <module>
    main()
  File "C:\Python27\Bot folder\frankie\client.py", line 140, in main
    firststock, html1 = checkRS()
  File "C:\Python27\Bot folder\frankie\client.py", line 105, in checkRS
    NSTC, TimeC = findNST(html)
  File "C:\Python27\Bot folder\frankie\client.py", line 50, in findNST
    Times = p.findall(NST[0])
IndexError: list index out of range

Can someone please fix this for me? I am not a programmer, just someone looking for a quick fix. If its a hard task please let me know im not trying to have someone waste their time on me :(.

Recommended Answers

All 8 Replies

If you want to fix this, you must say what the bot should do when it doesnt grab the time because it gets a blank page.

I would just need it to refresh again and then conitnue its same duty.

The first thing you can do is raise an exception when this error is detected, for example:

class CannotGrabTime(Exception):
    pass

def findNST(html):
    NST = re.findall(r'<td id="nst">(.*) am', html)
    if NST == []:
        NST = re.findall(r'<td id="nst">(.*) pm', html)
    if not NST:
        raise CannotGrabTime
    p = re.compile('\d+')
    Times = p.findall(NST[0])
    return NST, Times

The next thing to do is to catch the exception at an appropriate level and correct the bot's behavior at that level. It could be in funtion checkRS() or in funtion main(), for example in checkRS

def checkRS(...):
    ...
    try:
        NSTC, TimeC = findNST(html)
    except CannotGrabTime:
        # corrective code here
        ...

or in main()

def main(...):
    ...
    try:
        firststock, html1 = checkRS()
    except CannotGrabTime:
        # corrective code here
        ...

It all depends on the code in these functions

Thank you for the help. Im going to throw in the first code and se if it works. Is there anyone way I can +rep you on something for the help either way? :)

the old problem doesnt come up anymore but now I get this error

Traceback (most recent call last):
  File "C:\Python27\Bot folder\frankie\client.py", line 175, in <module>
    main()
  File "C:\Python27\Bot folder\frankie\client.py", line 146, in main
    secondstock, html2 = checksecondRS()
  File "C:\Python27\Bot folder\frankie\client.py", line 123, in checksecondRS
    secondstock, stockoii, html = get_stock()
  File "C:\Python27\Bot folder\frankie\client.py", line 26, in get_stock
    html = neo.get('/halloween/garage.phtml', referer = '')
  File "C:\Python27\Bot folder\frankie\classes\NeoAccount.py", line 90, in get
    res = self.opener.open(url)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Python27\lib\httplib.py", line 1030, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

Sorry to be a bother. Is there an easy fix to this?

You must not consider that the first problem is fixed by the above code, it is at most hidden or ignored as long as we don't write corrective code to tell the bot what to do when it can't grab the time.

In this new error, your program apparently fails to access an url related to this string '/halloween/garage.phtml'. Again, we don't know why this resource is not accessible nor why your code needs it. Therefore it is difficult to write a quick fix without the source code.

Edit: does it have anything to do with this: http://www.neopets.com/halloween/garage.phtml ?

yes it does. it refreshes there but sometimes it messes up not sure why

If needed I can supply the code. Only problem is it contains personal information so I would need to truth the person and have it done through Pm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.