Python Forum
[SOLVED] [Beautiful Soup] Replace tag.string from another file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Beautiful Soup] Replace tag.string from another file?
#1
Question 
Hello,

In an HTML page ("target"), I'd like to replace the body's text with the body from another file ("source").

Neither ".string", ".text", nor "str()" seems the right way to do it :-/ Does someone know?

Thank you.

""" target
<body>
	<div id="body">FILL_ME</div>
</body>
"""

#TypeError: decoding to str: need a bytes-like object, NoneType found
target.body.find('div', id='body').string = source.body.string

#&lt;
target.body.find('div', id='body').string = str(source.body)

#plain text, tags gone
target.body.find(id="body").string.replace_with(source.body.text)

#TypeError: decoding to str: need a bytes-like object, Tag found
target.body.find('div', id='body').string = source.body

#AttributeError: 'NoneType' object has no attribute 'replace_with'
target.body.find(string="FILL_ME").replace_with(source.body)
Reply
#2
Still stuck :-/

The goal is to 1) grab "<i>good stuff</i>" from the input, and 2) use it to replace "FILL_ME" in the output:

html_in = """
<body>
	<i>good stuff</i>
</body>
"""

html_out = """
<body>
	<div id="body">FILL_ME</div>
</body>
"""
soup_in = BeautifulSoup(html_in,"html.parser")
soup_out = BeautifulSoup(html_out,"html.parser")
body_copy = copy.copy(soup_in.body)
#print(body_copy)

#ValueError: Cannot replace an element with its contents when that element is not part of a tree.
#body_copy.unwrap()
#stuff = body_copy.unwrap()

#NONE stuff = body_copy.string
#PLAIN stuff = body_copy.text
#PLAIN stuff = body_copy.get_text()
#&lt; stuff = str(body_copy)
print(stuff)

#soup_out.find("div", id="body").string.replace_with(body_copy)
soup_out.find("div", id="body").string.replace_with(stuff)
print(soup_out)
Reply
#3
For others' benefit:

html_in = """
<body>
	<i>good stuff</i>
	
<i>more good stuff</i>

</body>
"""

html_out = """
<body>
	<div id="body">FILL_ME</div>
</body>
"""

soup_in = BeautifulSoup(html_in,"html.parser")
soup_out = BeautifulSoup(html_out,"html.parser")

new_body = soup_out.find('div', id='body')
new_body.clear()
new_body.extend(soup_in.body.contents)

print(soup_out)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Getting python's default 'printed' byte-string as string ? MvGulik 8 136 Apr-06-2026, 09:16 AM
Last Post: Dustbunny
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,524 May-28-2025, 07:56 AM
Last Post: Winfried
Information [SOLVED] [Beautiful Soup] How to deprettify? Winfried 3 1,307 May-11-2025, 05:32 PM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 967 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 3,817 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  [SOLVED] Sub string not found in string ? jehoshua 4 2,311 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,193 Oct-17-2024, 01:15 AM
Last Post: Winfried
Question [SOLVED] How to replace characters in a string? Winfried 2 1,957 Sep-04-2024, 01:41 PM
Last Post: Winfried
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 4,178 Aug-07-2024, 08:18 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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