BeautifulSoup - why is it printing file path and not the content
Question: I am trying to understand how BeautifulSoup works. Note that I am really new
to Python so I am probably missing something out.

I open a Python terminal and write this:

    
    
    from bs4 import BeautifulSoup
    import re
    ytchannel = '/home/XXX/Documents/test2'
    soup = BeautifulSoup(ytchannel)
    print(soup.prettify())
    

This is what I get:

    
    
    <html>
     <body>
      <p>
       /home/XXX/Documents/test2
      </p>
     </body>
    </html>
    

Why? It makes no sense to me. I just want the content of `test2`. I am writing
exactly what is written on the BeautifulSoup website.


Answer: You passed a string to `BeautifulSoup()`; sure it is a filename, but
`BeautifulSoup()` does not open filenames for you. It operates on strings or
open file-objects only.

Open the file first; `BeautifulSoup()` will read file objects if you pass
those in:

    
    
    with open(ytchannel) as infile:
        soup = BeautifulSoup(infile)
    

See [Making the
soup](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#making-the-soup).

