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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:43:51+00:00 2026-05-30T04:43:51+00:00

Say I have html code similar to this: <a href=http://example.org/>Stuff I do want</a> <p>Stuff

  • 0

Say I have html code similar to this:

<a href="http://example.org/">Stuff I do want</a>
<p>Stuff I don't want</p>

Using HTMLParser’s handle_data doesn’t differentiate between the link-text(stuff I do want)(Is this even the right term?) and the stuff I don’t want. Does HTMLParser have a built-in way to have handle_data return only link-text and nothing else?

  • 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-05-30T04:43:52+00:00Added an answer on May 30, 2026 at 4:43 am

    Basically you have to write a handle_starttag() method as well. Just save off every tag you see as self.lasttag or something. Then, in your handle_data() method, just check self.lasttag and see if it’s 'a' (indicating that the last tag you saw was an HTML anchor tag and therefore you’re in a link).

    Something like this (untested) should work:

    from HTMLParser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
    
        lasttag = None
    
        def handle_starttag(self, tag, attr):
            self.lasttag = tag.lower()
    
        def handle_data(self, data):
            if self.lasttag == "a" and data.strip():
                print data
    

    In fact it’s permissible in HTML to have other tags inside an <a...> ... </a> container. And there can also be anchors that contain text but aren’t links (no href= attribute). These cases can both be handled if desired. Again, this code is untested:

    from HTMLParser import HTMLParser
    
    class MyHTMLParser(HTMLParser):
    
        inlink = False
        data   = []
    
        def handle_starttag(self, tag, attr):
            if tag.lower() == "a" and "href" in (k.lower() for k, v in attr):
               self.inlink = True
               self.data   = []
    
        def handle_endtag(self, tag):
            if tag.lower() == "a":
                self.inlink = False
                print "".join(self.data)
    
        def handle_data(self, data):
            if self.inlink:
                self.data.append(data)
    

    HTMLParser is what you’d call a SAX-style parser, which notifies you of the tags going by but makes you keep track of the tag hierarchy yourself. You can see how complicated this can get just by the differences between the first and second versions here.

    DOM-style parsers are easier to work with for these kinds of tasks because they read the whole document into memory and produce a tree that is easily navigated and searched. DOM-style parsers tend to use more memory and be slower than SAX-style parsers, but this is much less important now than it was ten years ago.

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

Sidebar

Related Questions

Say I have jquery code like this: html += '<div class=index>' + item.index +
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let say I have this HTML code snippet: <div id=container> <div id=textContent>Text Content Te</div>
Let's say I have the following HTML code: <a href=/site/somesite/> somesite</a> My question is
Let's say I have the following minimalistic HTML code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Let's say I have the following in my HTML code: <select name=Currency id=Currency> <option
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:
Let's say we have two flash objects on the same html page. I want
Let's say I have the following HTML code <div id="outer"> <div id="inner">Hello World</div> </div>
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>

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.