Re: AttributeError: 'NoneType' object has no attribute 'replace_with'

636 views
Skip to first unread message

air_d...@verizon.net

unread,
Jan 24, 2013, 1:31:17 PM1/24/13
to beauti...@googlegroups.com
fyi: this is on osx.7:

> uname -a
Darwin toms-MacBook-Pro.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
..> python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin

interestingly, on fc17 i get the same errors, but it runs to completion & the output is correct:

$ uname -a 
Linux pNrHP 3.6.11-1.fc17.i686 #1 SMP Mon Dec 17 22:52:59 UTC 2012 i686 i686 i386 GNU/Linux
$ ./newmenus.py ../index.html
<div id="mininav"><!-- open mininav -->
                    <a class="urm" href="auditions.html"><img src="auditions.png" title="Now Auditioning"/></a>
                    <a class="urm" href="tickets.html"><img alt="tickets" src="tickets.gif" title="buy tickets"/></a>
   <a class="urm" href="individual.html#donors"><span class="urm">Donate</span></a>
   <a class="urm" href="bcshop.html"><span class="urm">Shop</span></a>
   <a class="urm" href="contact.php"><span class="urm">Contact</span></a>
                 </div>
mininav: <type 'NoneType'>
None
Traceback (most recent call last):
  File "./newmenus.py", line 34, in <module>
    bs=replacemenus(f.read())
  File "./newmenus.py", line 21, in replacemenus
    mininav.replace_with(newmininav)
AttributeError: 'NoneType' object has no attribute 'replace_with'
mininav: <class 'bs4.element.Tag'>
<div id="mininav"><!-- open mininav -->
                    <a href="auditions.html"><img src="auditions.png" title="Now Auditioning"/></a>
                    <a href="tickets.html"><img src="tickets.gif" title="buy tickets"/></a>
                    <a href="bcshop.html"><img src="shop.png"/></a>
                    <a href="press.html"><img src="press.png"/></a>
                    <a href="contact.php"><img src="contct.png"/></a>
                 </div>
../index.html ->test.new
 
i had to replace SoupStrainer('div') with .div...also note the 2 different output lines from 1 line of code:

    print "mininav:", type( mininav )

wtf?!?!?

air_d...@verizon.net

unread,
Jan 25, 2013, 9:36:49 AM1/25/13
to beauti...@googlegroups.com
apparently replace_with invalidates the soup object...see attached...i'm filing a bug

oops: attachment results in

An error (#340) occurred while communicating with the server.

here's the python session:

Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup,SoupStrainer
>>> newnav = BeautifulSoup(open("navigation.html"),parse_only=SoupStrainer('div'))
>>> fn="../index.html"
>>>
>>> f= open(fn, 'r')
>>> bs= BeautifulSoup(''.join(f.read()), "html5lib")
>>> f.close()
>>>
>>> nav=bs.find(id='navigation')
>>> print "nav:", type( nav )
nav: <class 'bs4.element.Tag'>
>>> nav.replace_with(newnav)
<div id="navigation"><!-- open navigation -->
...snip...
</ul>
</div>
>>> nav=bs.find(id='navigation')
>>> print "nav:", type( nav )
nav: <type 'NoneType'>


 

air_d...@verizon.net

unread,
Jan 25, 2013, 10:22:08 AM1/25/13
to beauti...@googlegroups.com

this is only a problem in osx...in fedora17 it works as expected....note the warning on SoupStrainer under linux, but not osx, both running the same versions of html5lib:
         beautifulsoup4 4.1.3 is already the active version in easy-install.pth
        html5lib 0.95 is already the active version in easy-install.pth)-:

https://bugs.launchpad.net/beautifulsoup/+bug/1105148


Python 2.7.3 (default, Jul 24 2012, 10:05:39)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2

Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup,

SoupStrainer
>>> newmininav = BeautifulSoup(open("mininav.html"),parse_only=SoupStrainer('div'))
/usr/lib/python2.7/site-packages/beautifulsoup4-4.1.3-py2.7.egg/bs4/builder/_html5lib.py:35: UserWarning: You provided a value for parse_only, but the html5lib tree builder doesn't support parse_only. The entire document will be parsed.
  warnings.warn("You provided a value for parse_only, but the html5lib tree builder doesn't support parse_only. The entire document will be parsed.")
>>> newmininav=newmininav.div


>>> fn="../index.html"
>>>
>>> f= open(fn, 'r')
>>> bs= BeautifulSoup(''.join(f.read()), "html5lib")
>>> f.close()
>>>

... mininav=bs.find(id='mininav')
>>> mininav.replace_with(newmininav)


<div id="mininav"><!-- open mininav -->
     <a href="auditions.html"><img src="auditions.png" title="Now Auditioning"/></a>
     <a href="tickets.html"><img src="tickets.gif" title="buy tickets"/></a>
     <a href="bcshop.html"><img src="shop.png"/></a>
     <a href="press.html"><img src="press.png"/></a>
     <a href="contact.php"><img src="contct.png"/></a>
     </div>

>>> mininav=bs.find(id='mininav')
>>> mininav

Lili Sun

unread,
Feb 17, 2015, 12:46:31 AM2/17/15
to beauti...@googlegroups.com
also suffer from this problem. does it get fixed or any solution in osx?

airdrummer

unread,
Feb 17, 2015, 8:59:24 AM2/17/15
to beauti...@googlegroups.com


Lili Sun wrote:
> also suffer from this problem. does it get fixed or any solution in osx?

i believe my problem was due to multiple python versions installed by macports
by my admin(sudoer) user acct, but not being picked up by my non-sudoer acct.


Reply all
Reply to author
Forward
0 new messages