html - make python variable triple quotation style -


i trying parse html has class name class="link", problem want read every line in variable parse it, should work triple quotation, how can make string variable triple quotation style. thanks.

from htmlparser import htmlparser  # create subclass , override handler methods class myhtmlparser(htmlparser):     def handle_starttag(self, tag, attrs):         print "encountered start tag:", tag     def handle_endtag(self, tag):         print "encountered end tag :", tag     def handle_data(self, data):         print "encountered data  :", data  # instantiate parser , fed html parser = myhtmlparser()  var = open('./index.html','r') strings = var.read()    parser.feed('<html><head><title>test</title></head>'         '<body><h1>parse me!</h1></body></html>') 

well, if read content local file, how can parse strings var?

index.html:

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head>     <meta http-equiv="content-type" content="text/html;charset=utf-8">     <title>document</title> </head> <body>     <div class="row">         <h1>hello world</h1>             <div class="row">                 <p>lorem ipsum dolor sit amet, consectetur adipisicing elit. id, excepturi, consequatur sed nobis facere veritatis tempore qui ipsum enim dignissimos!</p>             </div>     </div> </body> </html> 

if read html big string, how can parse it, want content in h1 tag. time.

   h1 = false     class myhtmlparser(htmlparser):        def handle_starttag(self, tag, attrs):           ## print "encountered start tag:", tag           if tag == 'h1':                  h1 = true        def handle_endtag(self, tag):           ## print "encountered end tag :", tag           if tag == 'h1':                  h1 = false        def handle_data(self, data):            ## print "encountered data  :", data            if h1:                  print data 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -