0

Is it possible to put In a variable a source code of an HTML file?

I mean, If I have test.html, so how can I take the source code of this html file and put it on a string ?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Roy Kuper
  • 107
  • 1
  • 11

1 Answers1

4

Just open the file and read it:

f = open('test.html', 'r')
html_string = f.read()
f.close()

or

with open('test.html', 'r') as f:  # with can auto close the file like f.close() does
    html_string = f.read()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Curry
  • 885
  • 5
  • 15