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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
""" 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:

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
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
Information [SOLVED] [Beautiful Soup] How to deprettify? Winfried 2 164 Yesterday, 03:46 PM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 308 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Replace values in Yaml file with value in dictionary PelleH 1 2,220 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  [SOLVED] Sub string not found in string ? jehoshua 4 1,444 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,615 Oct-17-2024, 01:15 AM
Last Post: Winfried
Question [SOLVED] How to replace characters in a string? Winfried 2 1,084 Sep-04-2024, 01:41 PM
Last Post: Winfried
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,886 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 10,442 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Replace a text/word in docx file using Python Devan 4 22,939 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 1,963 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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