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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:24:04+00:00 2026-05-28T02:24:04+00:00

I would like to know whats the difference between attrMap and attrs in BeautifulSoup

  • 0

I would like to know whats the difference between attrMap and attrs in BeautifulSoup? To be more specific, which tags have attrs and which have attrMap?

>>> soup = BeautifulSoup.BeautifulSoup(source)
>>> tag = soup.find(name='input')
>>> dict(tag.attrs)['type']
u'text'
>>> tag.attrMap['type']
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
  • 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-28T02:24:05+00:00Added an answer on May 28, 2026 at 2:24 am

    The attrMap field is an internal field in the Tag class. You should not use it in your code. You should instead use

    value = tag[key]
    tag[key] = value
    

    This maps internally to tag.attrMap[key], but only after __getitem__ and __setitem__ have made sure to initialize self.attrMap. This is done in _getAttrMap, which is nothing by a complicated dict(self.attrs) call. So for your code you’ll use

    >>> url = "http://stackoverflow.com/questions/8842224/"
    >>> soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url).read())
    >>> soup.find(name='input')
    >>> tag = soup.find(name='input')
    >>> tag['type']
    u'text'
    

    If you want to check for the existance of a given attribute, then you must use

    try:
        tag[key]
        # found key
    except KeyError:
        # key not present
    

    or

    if key in dict(tag.attrs):
        # found key
    else:
        # key not present
    

    As pointed out by Adam, this is because the __contains__ method on Tag searches the content, not the attributes, and so the more familiar key in tag doesn’t do what you would expect. This complexity arises because BeautifulSoup handles HTML tags with repeated attributes. So a normal map (dictionary) isn’t quite enough since the keys can be duplicated. But if you want to check if there is any key with a given name, then key in dict(tag.attrs) will do the right thing.

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

Sidebar

Related Questions

I would like to know whats the difference between TreeGrid and TreePanel components of
I would like to know what the difference between these instructions is: MOV AX,
I would like to know what is the difference between initializing a static member
I would like to know what is the difference between javascript:; and javascript:void(0); if
I would like to know What are the difference between folder-structure and package used
I would like know what is the difference between the Quartz Scheduler and JMS,
I would like to know what's the difference (advantages/disadvantages) between the following patterns. How
I Would like to know what are the difference between Using Xml Parser and
I would like to know the difference between two conventions: Creating an abstract base
I would like to know what is the difference between versioning approaches suggested by

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.