TypeError:' int' object是unsubscriptable - python

时间:2012-04-05 15:50:18

标签: python list int typeerror

我正在尝试创建一个小程序来获取随机网站并计算元素。

这是我的错误:

Traceback (most recent call last):
  File "elements counter.py", line 23, in <module>
    if elem[1] == string:
TypeError: 'int' object is unsubscriptable

这是我的代码:

from urllib2 import Request, urlopen, URLError

print 'Fetching URL..'

try:
    html = urlopen(Request("http://www.randomwebsite.com/cgi-bin/random.pl"))
except URLError:
    html = urlopen(Request("http://www.randomwebsitemachine.com/random_website/"))

print 'Loading HTML..'

ellist = [(None,None),]
isel = False
string = ''

for char in html.read():
    if char == '<':
        isel=True
    elif isel:
        if char == ' ' or char == '>':
            if string in ellist:
                for elem in ellist:
                    if elem[1] == string:
                        elem[0] += 1
            else:
                ellist += (1,string)
            isel = False
            string = ''
        else:
            string += char

print sorted(ellist, key = lambda tempvar: tempvar[0])

html.close()
raw_input()

如果您在代码中发现更多错误,请指出。

1 个答案:

答案 0 :(得分:2)

当你这样做时

            ellist += (1,string)

相同
            ellist.extend((1,string))

所以ellist看起来像

[(None, None), 1, string]

所以当你到达for循环中的第二个元素时,它是int而不是tuple

相反,做

            ellist.append((1,string))

或者,如果您真的想使用+=

            ellist += [(1,string)]

其余代码看起来基本正确,但请注意,您无法在引号或HTML注释中正确处理尖括号。如果要解析HTML,请使用其中一个HTML解析器,如Python的HTMLParser模块,lxml或BeautifulSoup。

相关问题