我在实例化类如:
时遇到错误GoogleCrawler('foods')
TypeError: __new__() takes exactly 4 arguments (2 given)
Process terminated with an exit code of 1
我使用的抽象类(abc)可能会产生问题。 我称这个班为:
from crawler.GoogleCrawler import GoogleCrawler
if __name__ == '__main__':
#w = GoogleCrawler(search_key='PKK')
GoogleCrawler('foods')
以下是代码:
BaseCrawler.py
class BaseCrawler(ABCMeta):
def __init__(self, search_key='', **kwargs):
if type(search_key) == str:
if search_key == '':
search_key = 'Kabe'
self.g_search_key_list = [search_key]
code continues ...
GoogleCrawler.py包含
from BaseCrawler import BaseCrawler
class GoogleCrawler(BaseCrawler):
def __init__(self, search_key='', **kwargs):
super().__init__(search_key, **kwargs)
# google search specific url parameters
self.search_url_prefix = kwargs.get('search_url_prefix', 'https://www.google.com.sg/search?q=')
self.search_url_postfix = kwargs.get('search_url_postfix','&source=lnms&tbm=isch&sa=X&ei=0eZEVbj3IJG5uATalICQAQ&ved=0CAcQ_AUoAQ&biw=939&bih=591')
# show more options
# options : 'id','class'
self.show_more_find_type = kwargs.get('show_more_find_type', 'id')
self.show_more_find_value = kwargs.get('show_more_find_value', 'smb')
code continues ...
答案 0 :(得分:5)
使用Python 2.x,您需要将元类指定为类的特殊属性。
class BaseCrawler(object):
__metaclass__ = ABCMeta
答案 1 :(得分:2)
除了Daniel Roseman建议的内容(向ELFCLASSNONE
添加__metaclass__ = ABCMeta
)之外,还需要在BaseCrawler
中添加对super()
的调用的参数,因为在Python中2它始终需要至少一个参数(在这种情况下需要两个参数)。
GoogleCrawler