Parse HTML files then edit them in python -
so problem want have website has single navbar.html file. can use add other pages in website. figured best way without javascript trying avoid because of compatibility issues (school website trying best compatibility). writing website school don't know going happen on want create automated system can parse folder html files go each , find <nav id="mainnav">.....</nav> , replace content between 2 tags content 1 central navbar.html can go in change navbar.html run script , auto update webpages.
now i'm not asking write code me have basic intermediate understanding of python if point me in right direction such maybe possible ways go this. help.
edit: use standard libraries easy possible move around preferably 1 *.py file
edit: please don't tell me go tool can't server not have server side programming languages best can edit local files upload static pages server.
edit: fixed on topic. hopefully.
i see lot of people criticizing trying do, , i'm going join them. horrible way implement navbar, , horrible procedural call 'updating' web site. adamant school isn't wise decision, , more time going wasted in end doing in archaic manner opposed also-free alternative php or python.
with said, question, because looks (based on commends , replies) implementing templating engine or dynamic web content beyond control, , not option. approach have navbar replacement script (lets call script.py) @ top of html directory, , sitting beside new navbar.html file.
first files, find files in child directories using os.walk:
import os root, dirs, files in os.walk(os.join(os.getcwd(), "html_dir"): file in files: # directories dir in dirs: # directories obviously, you'll want make recursive, applies html files in subfolders.
next, far replacement goes, i'd recommend reading in entirety of navbar.html file simple like:
with open('file', 'r') target_file: replacement_text = target_file.read() to actual replacement, i'd recommend regular expression. regular expressions vary depending on content, , may need tweaked depending on exact data being worked with. prone breaking if data changes.
import re new_html = re.sub(r'<nav id="mainnav">.*?</nav>', replacement_text, original_text) you'll need piece together, , make work yourself, feel that's solid approach problem. however, re-iterate: shudder @ thinking update website using method. also, backup files every time run this, failing results in unintended replacement/deletion of text.
Comments
Post a Comment