python程序是如何执行的?
带class的python程序是怎样执行的?
如
#!/usr/bin/python
import sgmllib,urllib
class MyParser(sgmllib.SGMLParser):
"A simple parser class."
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
"Initialise"
sgmllib.SGMLParser.__init__(self, verbose)
self.hyperlinks = []
self.images = []
self.title=''
self.inside_title=False
self.contents=[]
def start_title(self, attrs):
self.inside_title = True
def end_title(self):
self.inside_title = False
def handle_data(self, data):
if self.inside_title and data:
self.title = self.title + data + ' '
def start_a(self, attributes):
for name, value in attributes:
if name == "href":
self.hyperlinks.append(value)
def start_meta(self, attributes):
for name, value in attributes:
if name == "content":
self.contents.append(value)
def start_img(self, attributes):
for name, value in attributes:
if name == "src":
self.images.append(value)
def get_hyperlinks(self):
return self.hyperlinks
def get_images(self):
return self.images
def get_title(self):
return self.title.strip()
def get_countent(self):
return self.contents
# Get something to work with.
testurl = "http://www.baidu.com"
f = urllib.urlopen(testurl)
s = f.read()
f.close()
myparser = MyParser()
myparser.parse(s)
#output
print "web url is:" + testurl
#print "title:" + myparser.get_title()
countents = myparser.get_countent()
for countent in countents:
print "meta:" + countent
[解决办法]
你的程序从class声明结束后开始执行,第一个被执行的应该是:
testurl = "http://www.baidu.com"