AppleTattooGuy
AppleTattooGuy

Reputation: 1175

Parsing XML response: List index out of range error

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

Upvotes: 0

Views: 1251

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121992

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.

Upvotes: 5

Vorsprung
Vorsprung

Reputation: 34337

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)

Upvotes: 1

Related Questions