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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:15:41+00:00 2026-06-15T10:15:41+00:00

The code is from: Python if-statement based on content of HTML title tag from

  • 0

The code is from:
Python if-statement based on content of HTML title tag

from HTMLParser import HTMLParser

def titleFinder(html):
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            self.intitle = tag == "title"
        def handle_data(self, data):
            if self.intitle:
                self.title = data

    parser = MyHTMLParser()
    parser.feed(html)
    return parser.title

>>> print titleFinder('<html><head><title>Test</title></head>'
                '<body><h1>Parse me!</h1></body></html>')
Test

However, I got the following error message when the code below is run,

AttributeError: MyHTMLParser instance has no attribute ‘intitle’

How can i fix the error message? Any ideas?

Code:

from HTMLParser import HTMLParser
import urllib2

def titleFinder(html):
    intitle = False
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            self.intitle = tag == "title"
        def handle_data(self, data):
            if self.intitle:
                self.title = data

    parser = MyHTMLParser()
    parser.feed(html)
    return parser.title

response=urllib2.urlopen("https://stackoverflow.com/questions/13680074/attributeerror-xx-instance-has-no-attribute-intitle")
html= response.read()
print titleFinder(html)

The trackback is:

Traceback (most recent call last):
  File "D:\labs\test.py", line 19, in <module>
    print titleFinder(html)
  File "D:\labs\test.py", line 14, in titleFinder
    parser.feed(html)
  File "C:\Python27\lib\HTMLParser.py", line 108, in feed
    self.goahead(0)
  File "C:\Python27\lib\HTMLParser.py", line 142, in goahead
    if i < j: self.handle_data(rawdata[i:j])
  File "D:\labs\test.py", line 10, in handle_data
    if self.intitle:
AttributeError: MyHTMLParser instance has no attribute 'intitle'

[UPDATE]

I finally solved the problem! Thank you, Martijn Pieters!

from HTMLParser import HTMLParser
import urllib2

def titleFinder(html):
    class MyHTMLParser(HTMLParser):
        def __init__(self):
            HTMLParser.__init__(self)
            self.title = ''
            self.intitle = False  #!!!
        def handle_starttag(self, tag, attrs):
            self.intitle = tag == "title"
        def handle_data(self, data):
            if self.intitle:
                self.title = self.title+data #!!!

    parser = MyHTMLParser()
    parser.feed(html)
    return parser.title

response=urllib2.urlopen("https://stackoverflow.com/questions/13680074/attributeerror-xx-instance-has-no-attribute-intitle")

html= response.read()
print titleFinder(html)
  • 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-15T10:15:42+00:00Added an answer on June 15, 2026 at 10:15 am

    Your handle_data method is called before handle_starttag has been called, and there is no intitle attribute set at that moment.

    Simply add intitle = False to your class:

    class MyHTMLParser(HTMLParser):
        intitle = False
    
        # your methods
    

    handle_data is called for all text nodes in your document, including whitespace, so it’s not that unusual for it to be called before handle_starttag.

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

Sidebar

Related Questions

I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
my views.py file code: #!/usr/bin/python from django.template import loader, RequestContext from django.http import HttpResponse
I run the following code from a python interpreter, and expect the insert statement
Is it possible to overload the from/import statement in Python? For example, assuming jvm_object
I'm porting some code from Python 2 to 3. This is valid code in
when I write Python code from the interpreter I can type dir() to have
I want to execute python code from C# with following code. static void Main(string[]
I'm writing some code which involves running a few shell commands from Python and
I'm trying to call a java program from python using command line. The code
I'm calling a java program from my python code in the following way: subprocess.check_output([java,

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.