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


Groups > comp.lang.python > #81079

Re: Tag objects in Beautiful Soup

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'insert': 0.05; '"""': 0.07; 'assignment': 0.07; 'omit': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sub': 0.09; 'runs': 0.10; 'nameerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'soup': 0.16; 'url:packtpub': 0.16; 'wrote:': 0.18; 'example': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'tells': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'heading': 0.30; 'code': 0.31; 'getting': 0.31; '"",': 0.31; '>>>>': 0.31; 'anyone': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; '(most': 0.33; 'skip:b 30': 0.33; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'next': 0.36; 'should': 0.36; 'wrong': 0.37; 'thank': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'tell': 0.60; 'tag': 0.61; 'first': 0.61; 'name': 0.63; 're:': 0.63; 'beautiful': 0.68; 'book,': 0.68; 'reads': 0.68; 'home': 0.69; '&lt;a': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Tag objects in Beautiful Soup
Date Thu, 20 Nov 2014 16:02:08 +0100
Organization None
References <a7667688-96e9-4ba6-a343-42a757d784be@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd927e.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16021.1416495746.18130.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416495746 news.xs4all.nl 2934 [2001:888:2000:d::a6]:35509
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81079

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web