Why do I get a recursion error with BeautifulSoup and IDLE?

匿名 (未验证) 提交于 2019-12-03 02:43:01

问题:

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      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

回答1:

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)


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!