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

Simon Evans musicalhacksaw at yahoo.co.uk
Sat Jul 11 18:17:41 EDT 2015


Dear Programmers, 
Thank you for your advice regarding giving the console a current address in the code for it to access the html file. 

The console seems to accept the code to that extent, but when I input the two lines of code intended to access the location of a required word, the console rejects it re :

AttributeError:'NoneType' object has no attribute 'li' 

However the document 'EcologicalPyramid.html' does contain the words 'li' and 'ul', in its text. I am not sure as to how the input is arranged to output 'plants' which is also in the documents text, but that is the word the code is meant to elicit. 

I enclose the pertinent code as input and output from the console, and the html code for the document 'EcologicalPyramid.html'

Thank you in advance for your help. 

---------------------------------------------------------------------
>>> with open("C:\Beautiful Soup\ecologicalpyramid.html","r") as ecological_pyramid:
soup = BeautifulSoup("C:\Beautiful Soup\ecological_pyramid.html","lxml")
... producer_entries = soup.find("ul")
  File "<stdin>", line 2
    producer_entries = soup.find("ul")
                   ^
SyntaxError: invalid syntax
>>> producer_entries = soup.find("ul")
>>> print (producer_entries.li.div.string)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'li'
----------------------------------------------------------------------
prin<html>
<body>
<div class="ecopyramid">
<ul id= "producers">
<li class="producers">
<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>
<ul id ="primaryconsumers">
<li class="primaryconsumerlist">
<div class="name">deer</div>
<div class-"number">1000</div>
</li
<li class="primaryconsumerlist">
<div class="name">deer</div>
<div class="number">1000</div>
</li>
<li class="primaryconsumerlist">
<div class="name">rabbit</div>
<div class="number">2000</div>
</li>
</ul>
<ul id="secondaryconsumers">
<li class="secondary consumerlist">
<div class="name">fox</div>
<div class="number">100</div>
</li>
<li class=secondaryconsumerlist">
<div class="name">bear</div>
<div class="number">100</div>
</li>
</ul>
<ul id="tertiaryconsumers">
<li class="tertiaryconsumerslist">
<div class="name">lion</div>
<div class="number">80</div>
</li>
<li class="tertiaryconsumerlist">
<div class="name">tiger</div>
<div class="number">50</div>
</li>
</ul>
</body>
</html>



More information about the Python-list mailing list