gpt4 book ai didi

python - 为什么 BeautifulSoup 和 IDLE 会出现递归错误?

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

我正在按照教程尝试学习如何使用 BeautifulSoup。我正在尝试从我下载的 html 页面上的 url 中删除名称。我让它在这一点上工作得很好。

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("43rd-congress.html"))

final_link = soup.p.a
final_link.decompose()

links = soup.find_all('a')

for link in links:
print link

但是当我进入下一部分时

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("43rd-congress.html"))

final_link = soup.p.a
final_link.decompose()

links = soup.find_all('a')

for link in links:
names = link.contents[0]
fullLink = link.get('href')
print names
print fullLink

我收到这个错误

Traceback (most recent call last):
File "C:/Python27/python tutorials/soupexample.py", line 13, in <module>
print names
File "C:\Python27\lib\idlelib\PyShell.py", line 1325, in write
return self.shell.write(s, self.tags)
File "C:\Python27\lib\idlelib\rpc.py", line 595, in __call__
value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
File "C:\Python27\lib\idlelib\rpc.py", line 210, in remotecall
seq = self.asynccall(oid, methodname, args, kwargs)
File "C:\Python27\lib\idlelib\rpc.py", line 225, in asynccall
self.putmessage((seq, request))
File "C:\Python27\lib\idlelib\rpc.py", line 324, in putmessage
s = pickle.dumps(message)
File "C:\Python27\lib\copy_reg.py", line 74, in _reduce_ex
getstate = self.__getstate__
RuntimeError: maximum recursion depth exceeded

最佳答案

这是 IDLE 和 BeautifulSoup 的 NavigableString 对象(unicode 的子类)之间的错误交互。参见 issue 1757057 ;它已经存在了一段时间。

解决方法是先将对象转换为纯 unicode 值:

print unicode(names)

关于python - 为什么 BeautifulSoup 和 IDLE 会出现递归错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19578749/

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