Why doesn't input code return 'plants' as in 'Getting Started with Beautiful Soup' text (on page 30) ?

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Jul 12 20:23:05 CEST 2015


On 12/07/2015 18:33, Simon Evans wrote:
>
> Dear Peter Otten,
> I typed in (and did not copy and paste) the code as you suggested just now (6.28 pm, Sunday 12th July 2015), this is the result I got:
> ----------------------------------------------------------------------------
> 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.
>>>> from bs4 import BeautifulSoup
>>>> with open("C:\Beautiful Soup\ecologicalpyramid.html","r")as f:
> ... soup = BeautifulSoup(f,"lxml")
>    File "<stdin>", line 2
>      soup = BeautifulSoup(f,"lxml")
>         ^
> IndentationError: expected an indented block
>>>> soup = BeautifulSoup(f,"lxml")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> NameError: name 'f' is not defined
>>>>
> ----------------------------------------------------------------------------
> The first time I typed in the second line, I got the
> "Indentation error"
> the second time I typed in exactly the same code, I got the:
> "NameError:name 'f' is not defined"
>

You can tell that to the marines :)

 >>> with open("ecologicalpyramid.html","r")as f:
...     soup = BeautifulSoup(f)
...     producer_entries = soup.find("ul")
...     producer_entries
...
<ul id="producers">
<li class="producers">
</li><li class="producerlist">
<div class="name">plants</div>
<div class="number">100000</div>
</li>
<li class="producerlist">
<div class="name">algae</div>
<div class="number">100000</div>
</li>
</ul>
 >>>

Can I suggest that you slow down.  It strikes me that you're trying to 
run a marathon a day for a year before you can even walk.  For example 
is the file path in your call to open() correct?  Frankly I very much 
doubt it, although it is possible.

Perhaps you'd be more comfortable on the tutor mailing list?  If so see 
https://mail.python.org/mailman/listinfo/tutor or gmane.comp.python.tutor.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Python-list mailing list