954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HOW to read the html file

how to read the output of the html,

Actaully I am writing a small text as output whenever I invoke the html file

but when I am using urllib.read() or webbrowser.read() I am able to read the source of the html rather than its output.


I am a begineer So please kindly help me how to read the output of the html....

vamsicoolman
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

how to read the output of the html,

Actaully I am writing a small text as output whenever I invoke the html file

but when I am using urllib.read() or webbrowser.read() I am able to read the source of the html rather than its output.

I am a begineer So please kindly help me how to read the output of the html....

# for python 2.6
import urllib2
html = urllib2.urlopen('http://google.com').read()
print html


Edit: OR

filename = 'path\\to\\the\\html\\fil.html'
f = open(filename, "r").read()
print f
Krstevski
Junior Poster
110 posts since May 2009
Reputation Points: 17
Solved Threads: 5
 

I haven't understood what you are trying to do. Make HTML editor? Read HTML? or what? Can you please elaborate more?

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

Even the two replies which you gave read the source of the html rather giving me the ouput

If suppose I am having a html file with a.html

Hello World

WHen I invoke this html file it would give me an output Hello World

So I want script which would give the output of the html file.

It is not abt removing the tags and again giving me the text in html,
It is abt giving the output of the html file..

So please kindly give me a script of such kind which would give the output of the html
Thanks in advance..

vamsicoolman
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

An html file has no "output", it's just an html file. There are programs to convert an html file to a text file or a pdf file, you should google for that.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Please convert the attached file to .html,

actually it contains js script

The content in the place of the word hello will be varying.
So, when I invoke the html file it would give me an exception or the word "OK"

So is there any script in python which would help me to invoke the html file read status of the file i.e either an exception or the status OK
and write into a file...

Attachments test12js.txt (0.7KB)
vamsicoolman
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

So you wan't to strip out non HTML tags and leave only HTML?

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
import BeautifulSoup as bs

html = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Test JSON </TITLE>
  
   <script language="JavaScript">
      function checkJSON()
	  {
	  
	    try
	    {
			var v = eval(TextJSON.value);
			var OK='OK'
			document.write("OK");
			
	    }
	    catch (ex)
	    {
		  document.write("Error:"+ex);
          
	    }

		TextJSON.focus();
    }



	</script>
 </HEAD>
 <BODY >
 <table width="100%" border="0">
	 <tr>
	    <td>
           <textarea rows="15" cols="70" id="TextJSON" Style = "visibility:hidden">Hello</textarea> 
		  
        </td>
     </tr>
</table><script language="JavaScript">
checkJSON();
  </script>
</BODY>
</HTML>
"""

soup = bs.BeautifulSoup(html)
divs = soup.findAll('textarea')
children = divs[0].contents
print divs[0].string  # Hello

This find Hello in test12js.txt
Html dos not ever output anything as Gribouillis pointed out.
You parse html an find text like i did here.
You are better off learing more basic stuff about python and html.

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 



Hello

vamsicoolman
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

It's not at all clear, what do you mean when you say that you invoke an html file ?

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 
It's not at all clear, what do you mean when you say that you invoke an html file ?

I mean Invoke in the sense open the html file in the browser

For Example When I use the webbrowser.open() the html file will be opened in the browser. so now after opening in the browser then I will see either the exception or the ok as per the html file

so I want the script to even track that result and write into a text or either print in the shell...

vamsicoolman
Newbie Poster
14 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You