user6920987
user6920987

Reputation:

Python: if No XML tag , print 'Blank' along with Output

data="<database>
<zone1>
<name>
<firstname>John</firstname> 
<lastname>cena</lastname> 
<job>Administrator</job> 
<location>sunnyvale</location> 
<age>19</age> 
</name>
</zone1>

<zone2>
<name>
<firstname>mark</firstname> 
<job>Agent</job> 
<location>Bangalore</location> 
<age>22</age> 
</name>
</zone2>

<zone3>
<name>
<firstname>patrick</firstname> 
<lastname>phillips</lastname>
<job>Developer</job> 
<location>Philadelphia</location> 
<age>28</age> 
</name>
</zone3>

<zone4>
<name>
<firstname>patrick</firstname> 
<job>Developer</job> 
<location>Philadelphia</location> 
<age>28</age> 
</name>
</zone4>
</database>"    

import requests
from bs4 import BeautifulSoup
import lxml
soup = BeautifulSoup(data,lxml')

last_name=[v.get_text() if soup.find_all('lastname') else 'blank' for v in soup.find_all('lastname')]
print (last_name)

This is output for the above code.

['cena','phillips']

What is the wrong with the above code? Anyone tell me what needs to be changed for the above code? I want the below Output. (i.e. if XML tag doesn't exist 'blank' should be added)

['cena','blank','phillips','blank']

Upvotes: 0

Views: 52

Answers (1)

Rakesh
Rakesh

Reputation: 82755

Try:

from bs4 import BeautifulSoup
import lxml
soup = BeautifulSoup(data,'lxml')

last_name=[v.find('lastname').get_text() if v.find('lastname') else 'blank' for v in soup.find_all('name')]
print (last_name)
  • Find all name tag --> soup.find_all('name')
  • Check if name has lastname tag --> if v.find('lastname')

Upvotes: 2

Related Questions