在BeautifulSoup

时间:2016-01-11 15:51:50

标签: python beautifulsoup

我尝试在<div id="apb">之前插入评论。该错误提示了一种解决方法,确实有效。我在使用BeautifulSoup做错了什么,或者BeautifulSoup源是否有错误?我原始代码的最小可执行版本:

from bs4 import BeautifulSoup
from bs4 import Comment
soup = BeautifulSoup('<p>This</p>Insert here:***<div id="apb">Stuff</div>')
div = soup.find(id="apb")
comment = Comment('APB section')
div.insert_before(comment)

这会产生回溯:

AttributeError                            Traceback (most recent call last)
<ipython-input-20-09e7eb15e6f2> in <module>()
  4 div = soup.find(id="apb")
  5 comment = Comment('APB section')
----> 6 div.insert_before(comment)
  7

C:\Users\bbrown\AppData\Local\Enthought\Canopy\User\lib\site-packages\bs4\element.pyc in insert_before(self, predecessor)
353         # are siblings.
354         if isinstance(predecessor, PageElement):
--> 355             predecessor.extract()
356         index = parent.index(self)
357         parent.insert(index, predecessor)

C:\Users\bbrown\AppData\Local\Enthought\Canopy\User\lib\site-packages\bs4\element.pyc in extract(self)
232     def extract(self):
233         """Destructively rips this element out of the tree."""
--> 234         if self.parent is not None:
235             del self.parent.contents[self.parent.index(self)]
236

C:\Users\bbrown\AppData\Local\Enthought\Canopy\User\lib\site-packages\bs4\element.pyc in __getattr__(self, attr)
673             raise AttributeError(
674                 "'%s' object has no attribute '%s'" % (
--> 675                     self.__class__.__name__, attr))
676
677     def output_ready(self, formatter="minimal"):

AttributeError: 'Comment' object has no attribute 'parent'

我正在使用Python 2.7。我想我正在使用beautifulsoup4 v4.3.2;这是Canopy软件包管理器报告的版本,通过访问BeautifulSoup.__version__会导致AttributeError

我认为前面描述的错误可能是源代码中的错误的原因是我成功地添加了5行代码的解决方法:

comment.parent = None
comment.next_sibling = None
comment.next_element = None
comment.previous_sibling = None
comment.previous_element = None

我认为Comment构造函数会将这些值设置为None,或者element.py代码会测试属性是否存在,而不是测试与None的相等性。该错误是我的,还是BeautifulSoup源的问题?

1 个答案:

答案 0 :(得分:2)

这是一个相关的错误:

升级到当前最新的beautifulsoup4 4.4.1,其中包含修复程序:

pip install beautifulsoup4 --upgrade

或者,应用Martijn建议的workaround

comment = soup.new_string('APB section', Comment)
div.insert_before(comment)