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 01:06:46 CEST 2015


On 11/07/2015 23:17, Simon Evans wrote:
> 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")

Beautiful Soup takes a string or a file handle so as it's good  practise 
to use the "with open" construct this should do it:-

soup = BeautifulSoup(ecological_pyramid,"lxml")

but do you actually need the "lxml", with the simple parsing I've done 
in the past I've never used it?

-- 
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