Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:45:39+00:00 2026-06-14T02:45:39+00:00

I want to get whole html under a tag and using HTMLParser. I am

  • 0

I want to get whole html under a tag and using HTMLParser. I am able to currently get the data between the tags and following is my code

class LinksParser(HTMLParser):
  def __init__(self):
    HTMLParser.__init__(self)
    self.recording = 0
    self.data = ''

  def handle_starttag(self, tag, attributes):
    if tag != 'span':
      return
    if self.recording:
      self.recording += 1
      return
    for name, value in attributes:
      if name == 'itemprop' and value == 'description':
        break
    else:
      return
    self.recording = 1

  def handle_endtag(self, tag):
    if tag == 'span' and self.recording:
      self.recording -= 1

  def handle_data(self, data):
    if self.recording:
      self.data += data

I also want the html tags inside the input for example

<span itemprop="description">
<h1>My First Heading</h1>
<p>My first <br/><br/>paragraph.</p>
</span>

when provided as input would only give me the data with out tags. Is there any method with which I can get whole html between the tags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:45:40+00:00Added an answer on June 14, 2026 at 2:45 am

    One could use xml.etree.ElementTree.TreeBuilder to exploit etree API for finding/manipulating the <span> element:

    import sys
    from HTMLParser import HTMLParser
    from xml.etree import cElementTree as etree
    
    class LinksParser(HTMLParser):
      def __init__(self):
          HTMLParser.__init__(self)
          self.tb = etree.TreeBuilder()
    
      def handle_starttag(self, tag, attributes):
          self.tb.start(tag, dict(attributes))
    
      def handle_endtag(self, tag):
          self.tb.end(tag)
    
      def handle_data(self, data):
          self.tb.data(data)
    
      def close(self):
          HTMLParser.close(self)
          return self.tb.close()
    
    parser = LinksParser()
    parser.feed(sys.stdin.read())
    root = parser.close()
    span = root.find(".//span[@itemprop='description']")
    etree.ElementTree(span).write(sys.stdout)
    

    Output

    <span itemprop="description">
    <h1>My First Heading</h1>
    <p>My first <br /><br />paragraph.</p>
    </span>
    

    To print without the parent (root) <span> tag:

    sys.stdout.write(span.text)
    for child in span:
        sys.stdout.write(etree.tostring(child)) # add encoding="unicode" on Python 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using latest PHP. I want to parse HTML page to get data.
I am using the following code to get innerHTML of $(this) : var html=$(this).html();
My whole purpose of this is to get something looking like this: I want
I want to compile the Rigi source code but I get some error while
What I want: get a xml from the AppData to use What I code
Basically I want get data I already have accessed from javascript and passing it
My ajax (jquery) response (html) gives me a whole junk of html source code
how can I extract the whole html dom element incl. tags via php? eg.
Im grabbing details using preg_match_all but i have a wired html tag placement to
I'm Using AJAX to dynamically fetch data from my php page (which contains html)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.