运行时错误:使用Python 3.2 pickle.dump超出最大递归深度

3 投票
1 回答
2138 浏览
提问于 2025-04-17 13:53

我在下面的代码中遇到了上面的错误。这个错误发生在最后一行。请原谅我提到的内容,我只是想练习我的Python技能。=)

from urllib.request import urlopen
from bs4 import BeautifulSoup
from pprint import pprint
from pickle import dump

moves = dict()
moves0 = set()
url = 'http://www.marriland.com/pokedex/1-bulbasaur'
print(url)
# Open url
with urlopen(url) as usock:
    # Get url data source
    data = usock.read().decode("latin-1")
    # Soupify
    soup = BeautifulSoup(data)
    # Find move tables
    for div_class1 in soup.find_all('div', {'class': 'listing-container listing-container-table'}):
        div_class2 = div_class1.find_all('div', {'class': 'listing-header'})
        if len(div_class2) > 1:
            header = div_class2[0].find_all(text=True)[1]
            # Take only moves from Level Up, TM / HM, and Tutor
            if header in ['Level Up', 'TM / HM', 'Tutor']:
                # Get rows
                for row in div_class1.find_all('tbody')[0].find_all('tr'):
                    # Get cells
                    cells = row.find_all('td')
                    # Get move name
                    move = cells[1].find_all(text=True)[0]
                    # If move is new
                    if not move in moves:
                        # Get type
                        typ = cells[2].find_all(text=True)[0]
                        # Get category
                        cat = cells[3].find_all(text=True)[0]
                        # Get power if not Status or Support
                        power = '--'
                        if cat != 'Status or Support':
                            try:
                                # not STAB
                                power = int(cells[4].find_all(text=True)[1].strip(' \t\r\n'))
                            except ValueError:
                                try:
                                    # STAB
                                    power = int(cells[4].find_all(text=True)[-2])
                                except ValueError:
                                    # Moves like Return, Frustration, etc.
                                    power = cells[4].find_all(text=True)[-2]
                        # Get accuracy
                        acc = cells[5].find_all(text=True)[0]
                        # Get pp
                        pp = cells[6].find_all(text=True)[0]
                        # Add move to dict
                        moves[move] = {'type': typ,
                                       'cat': cat,
                                       'power': power,
                                       'acc': acc,
                                       'pp': pp}
                    # Add move to pokemon's move set
                    moves0.add(move)

    pprint(moves)
    dump(moves, open('pkmn_moves.dump', 'wb'))

为了找出这个错误,我尽量把代码简化到最少。问题可能很简单,但我就是找不到。在此期间,我通过把递归限制设置为10000来找到了一种解决办法。

1 个回答

10

我只是想给遇到这个问题的朋友们提供一个答案。具体来说,我在使用Django的会话功能时,遇到了从远程API获取的BeautifulSoup对象缓存的问题。

简单来说,BeautifulSoup节点不能被直接保存(也就是不能被“序列化”)。所以我选择把原始的字符串数据存储在我的对象里,并创建了一个方法,可以在需要的时候解析这些数据,这样就只保存了原始的字符串数据。

撰写回答