我正在尝试制作一个获取随机网站并计算元素的小程序。
这是我的错误:
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()
如果您在代码中发现更多错误,请指出。