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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:19:17+00:00 2026-05-26T15:19:17+00:00

I am trying to parse a website. I am using the HTMLParser module. The

  • 0

I am trying to parse a website. I am using the HTMLParser module. The problem is i want to parse the first <a href=""> after the comment: <!-- /topOfPage -->, but I don’t really know how to do it. So I have found in the documentation that there is an function which is called handle_comment, but I haven’t found out how to use it correctly. I have the following:

import HTMLParser

class LinkFinder(HTMLParser.HTMLParser):
def __init__(self, *args, **kwargs):
    # Can't use super() - HTMLParser is an old-style class
    HTMLParser.HTMLParser.__init__(self, *args, **kwargs)
    self.in_linktag = False
    self.url_cache = []
def handle_comment(self,data):
    if data == "topOfPage":
        print data
def handle_starttag(self, tag, attrs):
    if tag == "a" and any("href" == t[0] for t in attrs): # found link
        self.in_linktag = True
        self.url_cache.append([dict(attrs)['href']])
def handle_endtag(self, tag):
    if tag == "a" and self.in_linktag: # ignore '<a name=""...'
        self.in_linktag = False
def handle_data(self, data):
    if self.in_linktag:
        self.url_cache[-1].append(data)
TESTDATA = """
< html>
< body>
< div>
 < ul>
    < !-- /topOfPage --> 
< tr >
    < td class="empty-cell-left">&nbsp;</td>
    < td class="image">


    < a  href="http://test" rel="nofollow">
 < ul>
< /div>
< /body>
 < /html>
"""
def main():
lf = LinkFinder()
lf.feed(TESTDATA)
lf.close()
print lf.url_cache
if __name__ == "__main__":
    main()

How to do it?

  • 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-26T15:19:17+00:00Added an answer on May 26, 2026 at 3:19 pm

    You need an additional variable to indicate that the parser has just come past to the comment, so that you can save the reference from the first link after it.

    def __init__(self, *args, **kwargs):
        # ...
        self.first_link_after_comment = False
    

    Then, when you encounter the comment, the flag must be switched.

    def handle_comment(self, data):
        if data.strip() == '/topOfPage':
            self.first_link_after_comment = True
    

    When you handle an opening tag, you want to be sure to just make it pass by if the parsing has not passed over the comment

    def handle_starttag(self, tag, attrs):
        if not self.first_link_after_comment:
            return
        # ...
    

    Conversely, when you handle the closing tag, you want to acknowledge that the mission has been accomplished.

    def handle_endtag(self, tag):
        if tag == 'a' and self.in_linktag: # ignore '<a name=""...'
            self.in_linktag = False
            self.first_link_after_comment = False
    

    Finally, when you append data, just make sure that it’s not just a string that’s empty or contains white space only.

    def handle_data(self, data):
        if self.in_linktag and data.strip():
            self.url_cache[-1].append(data)
    

    And here you are.

    $ your_script.py
    [['http://test']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse an xml file using XmlReader but although I am
I'm trying to parse a bunch of webpages from an adult website using Ruby:
I have a problem when trying to parse a webpage using jSoup. If I
Trying to parse some XML but apparently this is too much for a lazy
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm trying to parse an INI file using C++. Any tips on what is
I am trying to parse an XML response from a website in C#. The
I'm trying to get the size of my website using php and ftp. I
I'm trying to create an HttpWebRequest/HttpWebResponse session with an ASP.NET website to later parse
I am trying to pull data from a website using objective-c. This is all

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.