python 2.7 and unicode (one more time)

Simon Evans musicalhacksaw at yahoo.co.uk
Tue Dec 2 17:40:59 EST 2014


Hi Peter Otten
re:
----------------------------------------------------------------------------
There is no assignment 

soup_atag = whatever 

but there is one to atag. The whole session should when you omit the 
offending line 

> atag = soup_atag.a 

or insert 

soup_atag = soup 

before it. 
----------------------------------------------------------------------------
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> html_atag = """<html><body><p>Test html a tag example</p>
... <a href="http://www.packtpub.com'>Home</a>
... <a href="http;//www.packtpub.com/books'.Books</a>
... </body>
... </html>"""
>>> soup = BeautifulSoup(html_atag,'lxml')
>>> atag = soup.aprint(atag)
>>> atag = soup.a
>>> print(atag)
<a href="http://www.packtpub.com'>Home</a>
<a href=" http="">
</a>
>>> type(atag)
<class 'bs4.element.Tag'>
>>> tagname = atag.name
>>> print tagname
a
>>> atag.name = 'p'
>>> print (soup)
<html><body><p>Test html a tag example</p>
<p href="http://www.packtpub.com'>Home</a>
<a href=" http="">
</p></body>
</html>
>>> atag.name = 'p'
>>> print(soup)
<html><body><p>Test html a tag example</p>
<p href="http://www.packtpub.com'>Home</a>
<a href=" http="">
</p></body>
</html>
>>> atag.name = 'a'
>>> print(soup)
<html><body><p>Test html a tag example</p>
<a href="http://www.packtpub.com'>Home</a>
<a href=" http="">
</a></body>
</html>
>>> soup_atag = soup
>>> atag = soup_atag.a
>>> print (atag['href'])
http://www.packtpub.com'>Home</a>
<a href=
>>>
----------------------------------------------------------------------------
Thank you.
Yours
Simon.




More information about the Python-list mailing list