Python Forum
[SOLVED] [Beautiful Soup] How to deprettify?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Beautiful Soup] How to deprettify?
#4
For others' benefit, here's how to do it in Beautiful Soup:

import sys
import os
import glob
import shutil
from bs4 import BeautifulSoup

ROOT = r"c:\temp"
os.chdir(ROOT)
for file in glob.glob("*.html"):
	print("Handling ", file)

	#save original file
	ORIGFILE = fr"{file}.orig"
	#grab original times
	mtime = os.stat(file).st_mtime
	atime = os.stat(file).st_atime
	tup = (atime, mtime)
	dest = shutil.copyfile(file, ORIGFILE)
	os.utime(ORIGFILE, tup)

	#Remove all carriage returns
	with open(file, "r") as f:
		dna = f.read().replace("\n", "")

	#trim each string
	soup = BeautifulSoup(dna,"lxml")
	_ = [s.replace_with(s.text.strip()) for s in soup.find_all(string=True)]
	#save soup back to file
	with open(file, 'w', encoding='utf-8') as outp:
		outp.write(str(soup))
	#Must close before updating time
	os.utime(file, tup)
Reply


Messages In This Thread
RE: [SOLVED] [Beautiful Soup] How to deprettify? - by Winfried - May-11-2025, 05:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 624 May-01-2025, 03:43 PM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 399 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Trouble selecting attribute with beautiful soup bananatoast 3 3,112 Jan-30-2022, 10:01 AM
Last Post: bananatoast
  I need help parsing through data and creating a database using beautiful soup username369 1 2,437 Sep-22-2021, 08:45 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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