3

我正在按照教程尝试学习如何使用 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
4

1 回答 1

5

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

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

print unicode(names)
于 2013-10-25T00:04:43.993 回答