user8229029
user8229029

Reputation: 1162

How to determine if XML tags, attributes exist using Python?

I'm trying to find out if xml contains the subtypemismatch="true" text in the following xml.

<boardgames termsofuse="https://boardgamegeek.com/xmlapi/termsofuse">
    <boardgame objectid="45987" subtypemismatch="true">
        <yearpublished/>

If I use BeautifulSoup with the following code, I can get to a 'true' or 'false', but most of the xml I need to read in does not contain the subtypemismatch text, which causes a "KeyError: 'subtypemismatch'" to occur. How do I determine if the xml has this text to begin with?

data = response.read()      
text = data.decode('utf-8') 
soup = BeautifulSoup(text,'xml')

if soup.find('boardgame')['subtypemismatch'] != 'true':
    do something....

Upvotes: 0

Views: 1557

Answers (1)

Carlos Mermingas
Carlos Mermingas

Reputation: 3892

To avoid getting KeyError, use get instead of brackets to access the attribute:

if soup.find('boardgame').get('subtypemismatch') != 'true':

If the element does not have the attribute, get returns None. You can also give it a default value:

if soup.find('boardgame').get('subtypemismatch', 'false') != 'true':

And you can also use has_attr to test for the existence of an attribute without getting its value:

soup = BeautifulSoup(text, 'xml')

for boardgame in soup.find_all('boardgame'):
    if boardgame.has_attr('subtypemismatch'):
        print('has attribute')
    else:
        print('does not have attribute')

Upvotes: 1

Related Questions