0

I got this script from a forum and it keeps coming up with the following error

Traceback (most recent call last):
  File "test.py", line 42, in <module> main()
  File "test.py", line 28, in main
    bot_response = objektid[0].toxml()
IndexError: list index out of range

I have searched around for an answer to this but, I cannot relate the answers to my code, maybe due to me being such a noob with python.

The script is as follows.

#!/usr/bin/python -tt

# Have a conversation with a PandaBot AI
# Author A.Roots

import urllib, urllib2
import sys
from xml.dom import minidom
from xml.sax.saxutils import unescape

def main():

  human_input = raw_input('You: ')
  if human_input == 'exit':
    sys.exit(0)

  base_url = 'http://www.pandorabots.com/pandora/talk-xml'
  data = urllib.urlencode([('botid', 'ebbf27804e3458c5'), ('input', human_input)])

  # Submit POST data and download response XML
  req = urllib2.Request(base_url)
  fd = urllib2.urlopen(req, data)

  # Take Bot's response out of XML
  xmlFile = fd.read()
  dom = minidom.parseString(xmlFile)
  objektid = dom.getElementsByTagName('that')
  bot_response = objektid[0].toxml()
  bot_response = bot_response[6:]
  bot_response = bot_response[:-7]
  # Some nasty unescaping
  bot_response = unescape(bot_response, {"&amp;apos;": "'", "&amp;quot;": '"'})

  print 'Getter:',str(bot_response)

  # Repeat until terminated
  while 1:
    main()

if __name__ == '__main__':
  print 'Hi. You can now talk to Getter. Type "exit" when done.'
  main()

Your help on this is greatly appreciated

1
  • Try printing objektid - it looks like you're getting an empty list, so there's nothing at index 0. Commented Aug 12, 2013 at 13:53

2 Answers 2

5

No element <that> was found:

objektid = dom.getElementsByTagName('that')

so the list is empty.

Testing your code, I get the message:

<result status="3" botid="ebbf27804e3458c5"><input>Hello world!</input><message>Failed to find bot</message></result>

which contains no such tags. The error message seems to indicate that the specific bot id you are using does not or no longer exist. Perhaps you need to sign up for a new bot of your own on the Pandorabots homepage?

I note that you are doing Some nasty unescaping. Why not grab the text nodes under that tag instead and let the DOM library take care of that for you?

You may want to look into the ElementTree API (included with Python) instead as it is easier to use.

Sign up to request clarification or add additional context in comments.

2 Comments

When I see @Martijn's question edit - I just close the page, no chances :)
Many thanks for your helpful answer, indeed I was using the wrong bot id. Again, thanks for your help.
1

The problem is here

   objektid = dom.getElementsByTagName('that')
   bot_response = objektid[0].toxml()

If the dom.getElementsByTagName returns nothing at all, then objektid[0], the first element of objektid will not exist. Hence the fault!

To get around it do something like

  objektid = dom.getElementsByTagName('that')
  if len(objektid) >= 0:
      bot_response = objektid[0].toxml()
      bot_response = bot_response[6:]
      bot_response = bot_response[:-7]
      # Some nasty unescaping
      bot_response = unescape(bot_response, {"&amp;apos;": "'", "&amp;quot;": '"'})
  else:
      bot_response = ""

  print 'Getter:',str(bot_response)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.