Reputation: 725
I want to read an HTML file in Python 3.4.3.
I have tried:
import urllib.request
fname = r"C:\Python34\html.htm"
HtmlFile = open(fname,'w')
print (HtmlFile)
This prints:
<_io.TextIOWrapper name='C:\\Python34\\html.htm' mode='w' encoding='cp1252'>
I want to get the HTML source so that I can parse it with beautiful soup.
Upvotes: 6
Views: 40427
Reputation: 2708
I was trying to read the saved HTML file in the folder. I tried code mentioned by Vikasa but was getting an error. So I changed the code and tried to read it again it worked for me. The code is as follows:
fname = 'page_source.html' #this html file is stored on the same folder of the code file
html_file = open(fname, 'r')
source_code = html_file.read()
print the html page using
source_code
It will print the content read from the page_source.html file.
Upvotes: 1
Reputation: 6950
You will have to read the contents of the file.
HtmlFile = open(fname, 'r', encoding='utf-8')
source_code = HtmlFile.read()
Upvotes: 14