2016-11-29 112 views
-1

我试图启动OOP并决定以这种方式重写脚本。一个网页有,我要保存如此链接框我做下面的代码从我的班级的Bs4继承方法

class webpage(BeautifulSoup): 

    def __init__(self, link, html, links): 

     self.link = link 
     driver = webdriver.PhantomJS() 
     driver.get(link) 
     self.html = driver.page_source 
     self.links = [] 

    def forty_pages(self): 

     soup = BeautifulSoup(html, 'html.parser') 
     link_box = soup.find('div', {'id': 'sliderBottom'}) 
     rest = link_box.find_all('a') 
     forty_links = [] 

     for i in rest: 
      try: 
       link = i.get('href') 
       forty_links.append(link) 
      except: 
       pass 
     self.links.append(x for x in forty_links) 

test = webpage(link=root) 
test.forty_pages() 

的问题是,它说

TypeError: module.__init__() takes at most 2 arguments (3 given) 

我感到困惑的是self.html时,应填写本身驱动程序返回包含html数据的字符串。任何人都可以阐明这一点吗? 编辑:据我所知,没有必要成分,但我不能从类中调用该模块BS4,所以我坚持就如何implrment这个...例如:

class rightmove_page(object): 

    def __init__(self, link): 
     self.link = link 

    def forty_pages(self): 
     driver = webdriver.PhantomJS() 
     html = driver.get(self.link) 
     soup = BeautifulSoup(html, 'html.parser') 
     print(soup) 

给错误:

Traceback (most recent call last): 
    File "/home/sn/Documents/Projects/House_Prices/class_pased.py", line 21, in <module> 
    test.forty_pages() 
    File "/home/sn/Documents/Projects/House_Prices/class_pased.py", line 17, in forty_pages 
    soup = BeautifulSoup(html, 'html.parser') 
TypeError: 'module' object is not callable 
+1

你认为那些其他参数会来自哪里?你没有它们直接调用它。 – jonrsharpe

+0

为什么你想在这里继承BeautifulSoup?似乎没有任何理由这样做;你没有使用任何继承的功能。 –

+0

我想使用一些BeautifulSoups的功能,我只是读到作文和导入某些Bs4方法 – entercaspa

回答

0

对不起你们,整理出来对其做,只是介绍BS4类中的方法,而不是试图以包括高清初始化类的部分的功能。你是对的Daniel Roseman谢谢你