猿问

Flask 应用程序超出 Python 最大递归深度

这是错误:


maximum recursion depth exceeded while calling a Python object

我正在使用beautifulsoup解析从中接收到的网页 HTML,requests然后将解析后的数据存储到Product类中。该函数通过从 调用一个线程来运行ThreadPoolExecutor()。


运行功能:


executor = ThreadPoolExecutor()

t2 = executor.submit(ScrapePageFull, PageHtml)

product = t2.result()

ScrapePageFull功能:


def ScrapePageFull(data):

soup = BeautifulSoup(data)

product = Product()


# Price

price = soup.find(DIV, {ID: DATA_METRICS})[ASIN_PRICE]

product.price = float(price)


# ASIN

ASIN = soup.find(DIV, {ID: DATA_METRICS})[ASIN_ASIN]

product.asin = ASIN


# Title

title = soup.find(META, {NAME: TITLE})[CONTENT]

product.title = title


# Price String

price_string = soup.find(SPAN, {ID: PRICE_BLOCK}).text

product.price_string = price_string


return product

这是Product课程:


class Product:

def __init__(self):

    self.title = None

    self.price = None

    self.price_string = None

    self.asin = None

    pass


# Getters

@property

def title(self):

    return self.title


@property

def price(self):

    return self.price


@property

def asin(self):

    return self.asin


@property

def price_string(self):

    return self.price_string


# Setters

@title.setter

def title(self, title):

    self.title = title


@price.setter

def price(self, price):

    self.price = price


@asin.setter

def asin(self, asin):

    self.asin = asin


@price_string.setter

def price_string(self, price_string):

    self.price_string = price_string

感谢您的帮助,谢谢。


ibeautiful
浏览 289回答 1
1回答

慕田峪4524236

你在这里进入无限递归:@propertydef title(self):    return self.title返回self.title与再次调用此函数相同,因为定义一个名为的函数title会覆盖变量self.title。这也是无限递归:@title.setterdef title(self, title):    self.title = title@title.setter将重新定义赋值,比如从这个函数self.title = title中调用。self.title.setter(title)对于和 也是一样self.price的。self.price_stringself.asin要解决此问题,请重命名您的变量:def __init__(...):   self._title = None@propertydef title(self):    return self._title
随时随地看视频慕课网APP

相关分类

Python
我要回答