为什么使用BeautifulSoup和IDLE获得recursion错误?
我正在按照教程来学习如何使用BeautifulSoup。 我正试图从我下载的html页面上的url中删除名称。 我已经在这方面做得很好。
from bs4 import BeautifulSoup soup = BeautifulSoup(open("43rd-congress.html")) final_link = soup.pa 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.pa 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
)之间的一个有问题的交互。 见问题1757057 ; 已经有一段时间了。
解决方法是首先将对象转换为一个普通的unicode值:
print unicode(names)