Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81077 > unrolled thread

Tag objects in Beautiful Soup

Started bySimon Evans <musicalhacksaw@yahoo.co.uk>
First post2014-11-20 06:31 -0800
Last post2014-11-20 22:49 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Tag objects in Beautiful Soup Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-11-20 06:31 -0800
    Re: Tag objects in Beautiful Soup Peter Otten <__peter__@web.de> - 2014-11-20 16:02 +0100
    Re: Tag objects in Beautiful Soup Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-20 22:49 +0000

#81077 — Tag objects in Beautiful Soup

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-11-20 06:31 -0800
SubjectTag objects in Beautiful Soup
Message-ID<a7667688-96e9-4ba6-a343-42a757d784be@googlegroups.com>
Re:'Accessing the Tag object from Beautiful Soup' (page 22-25 - Getting Started with Beautiful Soup)
So far the code to python27 runs as given in the book, re: - 
----------------------------------------------------------------------------
>>> 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.a
>>> print(atag)
<a href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
&lt;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'&gt;Home&lt;/a&gt;
&lt;a href=" http="">
</p></body>
</html>
----------------------------------------------------------------------------
then under the next Sub heading : 'Attributes of a Tag object'
text reads : 
atag = soup_atag.a
print (atag['href'])

#output
http://www.packtpub.com

however when I put this code to the console I get error returns at the first line re:- 
----------------------------------------------------------------------------
>>> atag = soup_atag.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'soup_atag' is not defined
>>>
----------------------------------------------------------------------------
Can anyone tell me where I am going wrong or where the text is wrong ? 
So far the given code has run okay, I have put to the console everything the text tells you to. 
Thank you for reading.
Simon Evans

[toc] | [next] | [standalone]


#81079

FromPeter Otten <__peter__@web.de>
Date2014-11-20 16:02 +0100
Message-ID<mailman.16021.1416495746.18130.python-list@python.org>
In reply to#81077
Simon Evans wrote:

> Re:'Accessing the Tag object from Beautiful Soup' (page 22-25 - Getting
> Started with Beautiful Soup) So far the code to python27 runs as given in
> the book, re: -
> 
----------------------------------------------------------------------------
>>>> 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.a
>>>> print(atag)
> <a href="http://www.packtpub.com'&gt;Home&lt;/a&gt;
> &lt;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'&gt;Home&lt;/a&gt;
> &lt;a href=" http="">
> </p></body>
> </html>
> 
----------------------------------------------------------------------------
> then under the next Sub heading : 'Attributes of a Tag object'
> text reads :

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.

> print (atag['href'])
> 
> #output
> http://www.packtpub.com
> 
> however when I put this code to the console I get error returns at the
> first line re:-
> 
----------------------------------------------------------------------------
>>>> atag = soup_atag.a
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'soup_atag' is not defined
>>>>
> 
----------------------------------------------------------------------------
> Can anyone tell me where I am going wrong or where the text is wrong ?
> So far the given code has run okay, I have put to the console everything
> the text tells you to. Thank you for reading.
> Simon Evans

[toc] | [prev] | [next] | [standalone]


#81136

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-20 22:49 +0000
Message-ID<m4lr65$vvt$3@dont-email.me>
In reply to#81077
On Thu, 20 Nov 2014 06:31:08 -0800, Simon Evans wrote:

> Can anyone tell me where I am going wrong or where the text is wrong ?
> So far the given code has run okay, I have put to the console everything
> the text tells you to. Thank you for reading.
> Simon Evans

Having looked at the ebook, there seems to be an error in the book. 
Unfortunately I'm not 100% sure what the error is.

However, it may be that:

atag = soup_atag.a

is meant to be:

atag = soup.a

There are also errors in the html itself in the ebook, the href of each 
of the urls is quoted as href="....' (mixing single and double quotes) 
and one of the urls has a semi-colon where a colon is expected, these 
seem to throw the parser.

These errors also appear in your html snippet:

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>"""

In addition to these errors in the source html, you seem to have replaced 
a '>' with a '.' on the second anchor tag in the html.

<a href="http;//www.packtpub.com/books'.Books</a>
.......................................^

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web