class myspider ( base.spiderFrame ):
def spider(max_num,url,road):
for page_num in range(1,max_num):
html='url'+'&page'+'=%d'%(page_num)
page=urllib.request.urlopen(html)
soup=BeautifulSoup(page,"lxml")
……
def click_event( self, event ):
url=self.urltxt.GetValue()
max_num=self.page_num.GetValue()
road=self.roadtxt.GetPath()
spider(max_num,url,road)
代码如上,一直提示spider这个函数未定义?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
class ClassTest(object):
"""docstring for ClassName"""
def __init__(self, arg=None):
super(ClassTest, self).__init__()
self.arg = arg
# 对象方法,需要传入固定参数--类对象self,需要通过类的对象进行调用
def normal(self,):
print('normal_method')
# 类方法,需要传入固定参数--类cls,可通过类或者类对象来调用
@classmethod
def class_method(cls,):
print('class_method')
# 静态方法,不需要传入固定参数,可通过类或者类对象来调用
@staticmethod
def static_method():
print('static_method')
def main():
test = ClassTest()
test.normal()
test.class_method()
ClassTest.class_method()
test.static_method()
ClassTest.static_method()
if __name__ == '__main__':
main()
A. 你可以看下这个类中方法的简单定义,来改写你的代码
B. 所以不管那种方式,在类中定义类方法,在调用的时候,需要通过类或者类对象进行调用,不能像 spider(max_num,url,road) 这样直接调用
C. 如果一定要用spider(max_num,url,road) 这种方式调用,建议直接把spider方法放在类外面,作为公共的函数来调用就行了
def spider(max_num,url,road): 改成 def spider(self,max_num,url,road):
注意定义里面的self
@staticmethod
def spider(max_num,url,road)