我一直在使用Python和geektools进行游戏,在整理代码并使用循环之前,我已使脚本工作。

现在,它不会显示lalala方法之后的任何内容。

我正在使用geektools 3.0.2开发Mac 10.8.1。

#!/usr/bin/python

#Simple script that downloads runescape adventures log
#and outputs it to console
# Ashley Hughes 16/SEP/2012

import sys
import urllib2 #For HTTP access
from time import localtime, strftime #Time data functions
from xml.dom.minidom import parseString #XML parser

def lalala(n):
    i = 0
    while(i <= n):
        xmlTag = dom.getElementsByTagName('description')[i].toxml()
        xmlData = xmlTag.replace('<description>','').replace('</description>','').replace('\t','').replace('\n','')
        #print (str(i) + ": " + xmlData)
        print(xmlData)
        i = i + 1

try:
    f = urllib2.urlopen("http://services.runescape.com/m=adventurers-log/rssfeed?searchName=SIG%A0ZerO")
    #f = urllib.urlopen("http://www.runescape.com/")
except Exception, e:
    print "Could not connect"
    sys.exit(1)
s = f.read()
f.close()

dom = parseString(s)

print strftime("%a, %d %b %Y %H:%M:%S", localtime())
print "Working"
lalala(6)
print "Still working"
sys.exit(0)

最佳答案

当您的代码“打印”到GeekTool时,您会遇到unicode-ascii问题。更改:

xmlTag = dom.getElementsByTagName('description')[i].toxml()


对此:

xmlTag = dom.getElementsByTagName('description')[i].toxml().encode('ascii', 'ignore')


这对于使用GeekTool 3.0.3的mac 10.8.1的我来说很好

http://docs.python.org/howto/unicode.html

09-04 21:07