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

Peter Otten __peter__ at web.de
Sun Jul 12 08:26:28 EDT 2015


Simon Evans wrote:

> Dear Peter Otten, thank you for your reply that I have not gone very far
> into the detail of which, as it seems Python console cannot recognise the
> name 'f' as given it, re output below :
> 
> 
> 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")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'f' is not defined
>>>>

Is that copy-and-paste? When I try to reproduce that I get

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
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

and when I indent the soup = ... line properly I don't get an error:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
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")
... 
>>> 





More information about the Python-list mailing list