我运行了这段代码并得到如下错误:
Traceback (most recent call last):
File "urllister.py", line 26, in <module>
for k in l: print k,"points to",l[k],"\n"
RuntimeError: dictionary changed size during iteration
我唯一要做的就是在第 27 行的 for 循环中打印
from sgmllib import SGMLParser
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.data = []
def start_a(self, attrs):
href = [v for k , v in attrs if k == 'href']
if href:
self.data.extend(href)
if __name__ == '__main__':
import urllib
sock = urllib.urlopen("http://diveintopython.org")
parser = URLLister()
html = sock.read()
parser.feed(html)
sock.close()
parser.close()
for url in parser.data: print url
l = locals()
for k in l:
print k,"points to",l[k],"\n"