gpt4 book ai didi

python - 运行时错误 : maximum recursion depth exceeded with Python 3. 2 pickle.dump

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:56 25 4
gpt4 key购买 nike

我在下面的代码中遇到了上述错误。错误发生在最后一行。请原谅主题,我只是在练习我的 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 来解决此问题。

最佳答案

只想为可能遇到此问题的任何其他人提供答案。具体来说,我通过远程 API 在 Django session 中缓存 BeautifulSoup 对象。

简短的回答是不支持酸洗 BeautifulSoup 节点。相反,我选择将原始字符串数据存储在我的对象中,并有一个动态解析它的访问器方法,因此只有原始字符串数据被 pickle。

关于python - 运行时错误 : maximum recursion depth exceeded with Python 3. 2 pickle.dump,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com