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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from bs4 import BeautifulSoup
 
"""
<?xml version="1.0" encoding="utf-8"?>
 <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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from bs4 import BeautifulSoup
 
"""
<?xml version="1.0" encoding="utf-8"?>
 <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)
1
2
3
4
5
6
7
8
9
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
 <Document>
  <name>
   hello
  </name>
 </Document>
</kml>
if search eg for soup.select_one('kml > Document > name_99')
1
2
3
4
5
6
7
8
9
10
11
12
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
 <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 are some elements ignored? Winfried 5 1,793 Sep-04-2024, 09:34 AM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Why attribute not found? Winfried 0 1,307 Mar-11-2023, 10:00 PM
Last Post: Winfried
  compare and find the nearest element ? mr_gentle_sausage 4 2,088 Jan-15-2023, 07:11 AM
Last Post: DPaul
  Find (each) element from a list in a file tester_V 3 2,266 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 2,741 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,942 Aug-19-2022, 08:07 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 3,225 Aug-17-2022, 03:58 PM
Last Post: Winfried
  read a text file, find all integers, append to list oldtrafford 12 10,275 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to find the second lowest element in the list? Anonymous 3 4,512 May-31-2022, 01:58 PM
Last Post: Larz60+
  [Solved:] How to use pd.append() ? ju21878436312 3 2,104 Mar-01-2022, 07:41 AM
Last Post: ju21878436312

Forum Jump:

User Panel Messages

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