为什么使用 BeautifulSoup 和 IDLE 时会出现递归错误? [英] Why do I get a recursion error with BeautifulSoup and IDLE?

查看:30
本文介绍了为什么使用 BeautifulSoup 和 IDLE 时会出现递归错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 BeautifulSoup 的教程.我正在尝试从我下载的 html 页面上的 url 中删除名称.到目前为止,它运行良好.

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

我收到此错误

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

推荐答案

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

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.

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

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

print unicode(names)

这篇关于为什么使用 BeautifulSoup 和 IDLE 时会出现递归错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆