Python Forum
[SOLVED] [Beautifulsoup] Find if element exists, and edit/append?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Beautifulsoup] Find if element exists, and edit/append?
#1
Hello,

I read BS's documentation, but can't figure out something very simple: Find if an element exists at a specific location in the tree; If it exists, change its text; If it doesn't, insert it right below the parent.

from bs4 import BeautifulSoup 

"""
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>
   EDIT ME
  </name>
 </Document>
</kml>
"""
soup = BeautifulSoup(open('input.kml', 'r'), 'xml')

#BAD name = soup.kml.Document.name
name = soup.select('kml > Document > name') 
if name:
	print("Found")
	name.string="New name" #no change !
	name.string.replace_with("New name") #Error
else:
	print("Not found")
	name = soup.new_tag("name")
	name.string = "New tag"

	#doc= soup.kml.Document
	#doc = soup.select('kml.Document')
	doc = soup.select('kml > Document') 
	doc.append(name) #No change !

print(soup.prettify())
Thank you.
Reply
#2
If a tag tag doesn't exits it will give AttributeError,then catch this error and insert a new tag.
from bs4 import BeautifulSoup

"""
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>
   EDIT ME
  </name>
 </Document>
</kml>
"""

soup = BeautifulSoup(open('input.xml'), 'xml')
try:
    name = soup.select_one('kml > Document > name')
    name.string = 'hello'
except (AttributeError, NameError):
    doc = soup.find('Document')
    new_tag = soup.new_tag("car")
    new_tag.string = "Blue color"
    doc.append(new_tag)
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>
   hello
  </name>
 </Document>
</kml>
if search eg for soup.select_one('kml > Document > name_99')
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 <Document>
  <name>
   EDIT ME
  </name>
  <car>
   Blue color
  </car>
 </Document>
</kml>
Reply
#3
Thanks much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why attribute not found? Winfried 0 745 Mar-11-2023, 10:00 PM
Last Post: Winfried
  compare and find the nearest element ? mr_gentle_sausage 4 1,030 Jan-15-2023, 07:11 AM
Last Post: DPaul
  Find (each) element from a list in a file tester_V 3 1,204 Nov-15-2022, 08:40 PM
Last Post: tester_V
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,508 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,112 Aug-19-2022, 08:07 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 1,975 Aug-17-2022, 03:58 PM
Last Post: Winfried
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to find the second lowest element in the list? Anonymous 3 1,995 May-31-2022, 01:58 PM
Last Post: Larz60+
  [Solved:] How to use pd.append() ? ju21878436312 3 1,282 Mar-01-2022, 07:41 AM
Last Post: ju21878436312
  labels.append(self.classes.index(member.find('name').text)) hobbyist 1 1,911 Dec-15-2021, 01:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020