2016-11-29 110 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謝謝你