Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'messages.': 0.05; 'subject:Python': 0.06; 'indicating': 0.07; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'run,': 0.09; 'typed': 0.09; 'python': 0.11; 'windows': 0.15; 'ahead,': 0.16; 'interpreter,': 0.16; 'nameerror:': 0.16; 'programmers,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'soup': 0.16; 'subject: \n ': 0.16; 'subject:object': 0.16; 'syntaxerror:': 0.16; "would've": 0.16; 'wrote:': 0.18; 'bit': 0.19; 'file,': 0.19; 'input': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'received:comcast.net': 0.24; 'file.': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'am,': 0.29; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; '"",': 0.31; '>>>>': 0.31; 'subject:that': 0.31; 'file': 0.32; 'run': 0.32; 'quite': 0.32; '(most': 0.33; 'proceed': 0.33; 'maybe': 0.34; 'subject:with': 0.35; "can't": 0.35; 'point.': 0.35; 'but': 0.35; 'next': 0.36; 'wrong': 0.37; 'nov': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'skip:b 40': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'blank': 0.60; 'hope': 0.61; 'gone': 0.61; 'skip:o 30': 0.61; 'name': 0.63; 'more': 0.64; 'dear': 0.65; 'beautiful': 0.68; 'invalid': 0.68; 'prompt': 0.68; '(according': 0.84; 'console,': 0.84; 'plants': 0.84; 'subject:made': 0.84; '2013,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Date: Sun, 14 Dec 2014 11:36:47 -0500 References: <01937423-e700-4825-a502-568dcb2d5324@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <01937423-e700-4825-a502-568dcb2d5324@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418575028 news.xs4all.nl 2855 [2001:888:2000:d::a6]:53461 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82512 On 12/14/14 11:15 AM, Simon Evans wrote: > Dear Python programmers, > Having input the line of code in text: > cd Soup > to the Windows console, and having put the file 'EcologicalPyramid.html' into the Directory 'Soup', on the C drive, in accordance with instructions I input the following code to the Python console, as given on page 30 of 'Getting Started with Beautiful Soup': > ---------------------------------------------------------------------------- > 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("ecologicalpyramid.html","r") as ecological_pyramid: > ... soup = BeautifulSoup(ecological_pyramid,"lxml") > ... producer_entries = soup.find("ul") > > ^ > SyntaxError: invalid syntax This SyntaxError is indicating that none of the code you have just typed was run. The reason it's a syntax error is because the interactive prompt is a bit simplistic about how to handle multiple statements. It wants a blank line after the with-clause. Because none of these lines were run, you never opened your HTML file, never parsed it, and never assigned to the name "soup". >>>> producer_entries = soup.find("ul") > Traceback (most recent call last): > File "", line 1, in > NameError: name 'soup' is not defined >>>> > ^ > ---------------------------------------------------------------------------- > so I cannot proceed with the next line withh would 've been : > ---------------------------------------------------------------------------- > print(producer_entries.li.div.string) > ---------------------------------------------------------------------------- > which would've given (according to the book) the output: > --------------------------------------------------------------------------- > plants > > Maybe that is getting a bit far ahead, but I can't quite see where I have gone wrong - 'soup' has been defined as an object made of file 'EcologicalPyramid.html > > I hope you can help me on this point. For complex code experiments, it's better to put the code in a file, and run the file. But if you do want to use the interactive interpreter, enter code carefully, and watch out for the error messages. -- Ned Batchelder, http://nedbatchelder.com