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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:07:12+00:00 2026-05-13T15:07:12+00:00

I am using BeautifulSoup to parse XML: xml = <person> <first_name>Matt</first_name> </person> soup =

  • 0

I am using BeautifulSoup to parse XML:

xml = """<person>
<first_name>Matt</first_name>
</person>"""

soup = BeautifulStoneSoup(xml)
first_name = soup.find('first_name').string
last_name = soup.find('last_name').string

But I have a problem when there is no last_name, because it chokes. Sometimes the feed has it, and sometimes it doesn’t. How do I prevent it from choking?

I don’t want to use try/except statements. I also do not want to use if/else statements. (Since it’ll double the lines of the already-very-long code if I have those statements).

Is there any way to just return “None” if there is no “last_name”?

  • 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-13T15:07:12+00:00Added an answer on May 13, 2026 at 3:07 pm
    last_name = soup.find('last_name') and soup.find('last_name').string
    

    Very silly, but it does meet your equally silly stated restriction (no if). A bit less silly:

    last_name_node = soup.find('last_name')
    last_name = last_name_node and last_name_node.string
    

    and:

    last_name = getattr(soup.find('last_name'), 'string', None)
    

    These two don’t have the same overhead as the first. I think a simple if is more readable than any of these, though.

    • 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 a simple html table using beautifulsoup but I have
I have this code that fetches some text from a page using BeautifulSoup soup=
I'm using BeautifulSoup to parse some HTML. Let's say I have the following HTML
I'm using BeautifulSoup to parse through data on baseball-reference.com and it works find for
I have some random HTML and I used BeautifulSoup to parse it, but in
I am trying to parse webpages using urllib2, BeautifulSoup and Python 2.7. The problem
I have a webpage that I read using Python and BeautifulSoup, say soup=BeautifulSoup(urllib2.urlopen(site)) .
while using beautifulsoup to parse a table in html every other row starts with
I am trying to parse html using BeautifulSoup to try and extract the webpage
I'm trying to Parse the following HTML pages using BeautifulSoup (I'm going to parse

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.