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 8348839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:48:37+00:00 2026-06-09T07:48:37+00:00

I need to get the text inside the two elements into a string: source_code

  • 0

I need to get the text inside the two elements into a string:

source_code = """<span class="UserName"><a href="#">Martin Elias</a></span>"""

>>> text
'Martin Elias'

How could I achieve this?

  • 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-09T07:48:38+00:00Added an answer on June 9, 2026 at 7:48 am

    I searched “python parse html” and this was the first result:
    https://docs.python.org/2/library/htmlparser.html

    This code is taken from the python docs

    from HTMLParser import HTMLParser
    
        # create a subclass and override the handler methods
        class MyHTMLParser(HTMLParser):
            def handle_starttag(self, tag, attrs):
                print "Encountered a start tag:", tag
            def handle_endtag(self, tag):
                print "Encountered an end tag :", tag
            def handle_data(self, data):
                print "Encountered some data  :", data
    
        # instantiate the parser and fed it some HTML
        parser = MyHTMLParser()
        parser.feed('<html><head><title>Test</title></head>'
                    '<body><h1>Parse me!</h1></body></html>')
    

    Here is the result:

    Encountered a start tag: html
    Encountered a start tag: head
    Encountered a start tag: title
    Encountered some data  : Test
    Encountered an end tag : title
    Encountered an end tag : head
    Encountered a start tag: body
    Encountered a start tag: h1
    Encountered some data  : Parse me!
    Encountered an end tag : h1
    Encountered an end tag : body
    Encountered an end tag : html
    

    Using this and by looking at the code in HTMLParser I came up with this:

    class myhtmlparser(HTMLParser):
        def __init__(self):
            self.reset()
            self.NEWTAGS = []
            self.NEWATTRS = []
            self.HTMLDATA = []
        def handle_starttag(self, tag, attrs):
            self.NEWTAGS.append(tag)
            self.NEWATTRS.append(attrs)
        def handle_data(self, data):
            self.HTMLDATA.append(data)
        def clean(self):
            self.NEWTAGS = []
            self.NEWATTRS = []
            self.HTMLDATA = []
    

    You can use it like this:

    from HTMLParser import HTMLParser
    
    pstring = source_code = """<span class="UserName"><a href="#">Martin Elias</a></span>"""
    
    
    class myhtmlparser(HTMLParser):
        def __init__(self):
            self.reset()
            self.NEWTAGS = []
            self.NEWATTRS = []
            self.HTMLDATA = []
        def handle_starttag(self, tag, attrs):
            self.NEWTAGS.append(tag)
            self.NEWATTRS.append(attrs)
        def handle_data(self, data):
            self.HTMLDATA.append(data)
        def clean(self):
            self.NEWTAGS = []
            self.NEWATTRS = []
            self.HTMLDATA = []
    
    parser = myhtmlparser()
    parser.feed(pstring)
    
    # Extract data from parser
    tags  = parser.NEWTAGS
    attrs = parser.NEWATTRS
    data  = parser.HTMLDATA
    
    # Clean the parser
    parser.clean()
    
    # Print out our data
    print tags
    print attrs
    print data
    

    Now you should be able to extract your data from those lists easily. I hope this helped!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with a weak relation. I need get a text value
I need to get the text that is inside a element. I can only
I have the following classes: public class Note { public string Text { get;
I need to get the text in a RichTextBox while in another thread. I
I need to get the text changed event of tooltip. But I cant see
In javascript I need to get the text within the custom tag. For example
I need to get the Portuguese text content out of an Excel file and
I need to get a list of tags in a text, make their contents
I need to get lots of data (file select , text, date ,...) from
i have text like this div bla-bla end div i need to get only

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.