为什么使用 BeautifulSoup 和 IDLE 会出现递归错误?

发布于 2024-11-28 16:08:49 字数 1478 浏览 1 评论 0原文

我正在按照教程尝试学习如何使用 BeautifulSoup。我正在尝试从我下载的 html 页面上的网址中删除名称。到目前为止我的效果很好。

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

I am following a tutorial to try to learn how to use BeautifulSoup. I am trying to remove names from the urls on a html page I downloaded. I have it working great to this point.

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

but when I enter this next part

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

I get this error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-12-05 16:08:49

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

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

print unicode(names)

This is a buggy interaction between IDLE and BeautifulSoup's NavigableString objects (which subclass unicode). See issue 1757057; it's been around for a while.

The work-around is to convert the object to a plain unicode value first:

print unicode(names)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文