我从一个论坛得到这个脚本,它不断出现以下错误
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
我已经四处寻找答案,但我无法将答案与我的代码联系起来,可能是因为我是 python 的菜鸟。
脚本如下。
#!/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, {"&apos;": "'", "&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()
非常感谢您对此的帮助