【问题标题】:BeautifulSoup recursion error calling tag.string [duplicate]BeautifulSoup递归错误调用tag.string [重复]
【发布时间】:2012-12-20 02:38:06
【问题描述】:

这是一个我似乎无法弄清楚的非常奇怪的错误。

import urllib2
from bs4 import BeautifulSoup

url = 'http://www.crummy.com/software/BeautifulSoup/bs4/doc/'
soup = BeautifulSoup(urllib2.urlopen(url))

print soup.title

返回

<title>Beautiful Soup Documentation — Beautiful Soup 4.0.0 documentation</title>

正如预期的那样,但是如果我将其更改为“print soup.title.string”(应该返回上面的所有内容减去 html 标记)我得到

Traceback (most recent call last):
  File "C:\Users\MyName\Desktop\MyProgram\Python\test.py", line 7, in <module>
    print soup.title.string
  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

我环顾四周,找不到遇到此错误的其他人。有什么建议吗?

编辑:所以我在其他页面上尝试了相同的代码,效果更好。例如,google.com 就可以工作。这意味着它与页面的构造有关。

【问题讨论】:

  • 在 4.0.1 中为我工作;你的版本号是多少?
  • 4.1.3,他们提供的最新版本。我会尝试返回几个版本并进行测试。
  • 可能不是这样——我刚刚升级到 4.1.3,它仍然对我有用。 :-/
  • 是的,我尝试了 4.1.0 和 4.0.1 并且都给出了相同的错误。
  • 请参阅 this question 以了解其他人的报告。

标签: python beautifulsoup


【解决方案1】:

也许问题是因为它包含 non_ASCII 字符。 将您的打印语句修改为此

print soup.title.string.encode('ascii','ignore')

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多