我正在尝试启动OOP并决定以这种方式重写脚本。一个网页有一个我要保存的链接框,所以我做了以下代码
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,所以我不知道如何实现这个...例如:
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
答案 0 :(得分:0)