Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading an HTML File from Folder in Python

Tags:

python

file-io

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.

like image 517
Jithin P Avatar asked Sep 13 '15 07:09

Jithin P


1 Answers

You will have to read the contents of the file.

HtmlFile = open(fname, 'r', encoding='utf-8')
source_code = HtmlFile.read() 
like image 131
Vikas Ojha Avatar answered Oct 05 '22 23:10

Vikas Ojha