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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:48:37+00:00 2026-06-14T23:48:37+00:00

I have the following problem: I would like to parse html files and get

  • 0

I have the following problem:

I would like to parse html files and get links from the html file. I can get links with the following code:

class MyHTMLParser(HTMLParser):
    links=[]
    def __init__(self,url):
        HTMLParser.__init__(self)
        self.url = url

    def handle_starttag(self, tag, attrs):
        try: 
            if tag == 'a':
                for name, value in attrs:
                    if name == 'href':
                        if value[:5]=="http:":
                            self.links.append(value)
        except: 
            pass

But I dont want to get audio files, video files, etc. I only want to get html links. How can I do that?

  • 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-14T23:48:39+00:00Added an answer on June 14, 2026 at 11:48 pm

    I can check link ending and if it is particular format I can avoid
    appending that link to the list. Is there other way?

    You could look at the 'Content-Type' header:

    import urllib2
    url = 'https://stackoverflow.com/questions/13431060/python-html-parsing'
    req = urllib2.Request(url)
    req.get_method = lambda : 'HEAD'    
    response = urllib2.urlopen(req)
    content_type = response.headers.getheader('Content-Type')
    print(content_type)
    

    yields

    text/html; charset=utf-8
    

    Many thanks to @JonClements for req.get_method = lambda : 'HEAD'. More info on this and alternate methods for sending a HEAD request can be found here.

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

Sidebar

Related Questions

i have the following problem: i would like to create a footer. that footer
Ok, I have the following problem: I would like to scroll an overflowing ListBox
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem, from c++ I send huge string[] to java. huge
I have following code: string date = 13.04.2012; string date2 = (DateTime.Parse(date).AddDays(1)).ToString(); This is
I have class foo that contains a std::auto_ptr member that I would like to
I have spent the day researching Android RSS Readers. I would like to parse
What I have now is the following code: Tutorial tutorial = (from tutorial in
I would like some advice. I have encountered the following error in Python 2.6:
I have the following code so far: import sys from Tkinter import * import

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.