I have the following Python code:
content = webpage.content
soup = Soup(content, 'html.parser')
app_url = scheme + app_identity.get_default_version_hostname() + '/'
for link in soup.find_all(href = True):
if scheme in link['href']:
link['href'] = link['href'].replace(scheme, app_url)
logging.info('@MirrorPage | Updated link: %s', link['href'])
else:
link['href'] = input_url + link['href'].strip('/')
logging.info('@MirrorPage | Updated asset: %s', link['href'])
# https://stackoverflow.com/questions/15455148/find-after-replacewith-doesnt-work-using-beautifulsoup/19612218#19612218
#soup = Soup(soup.renderContents())
# https://stackoverflow.com/questions/14369447/how-to-save-back-changes-made-to-a-html-file-using-beautifulsoup-in-python
content = soup.prettify(soup.original_encoding)
and render my HTML like so:
self.response.write(Environment().from_string(unicode(content, errors = 'ignore')).render())
Where app_identity
is from Google App Engine, and jinja2
is used for templating/rendering. I've tried everything I can to write the modified HTML back to the content
variable so that the proper webpage is rendered. How can I write any changes I make back properly? I have tried to use replaceWith
where appropriate, but that doesn't seem to do the trick. Am I doing anything fundamentally wrong?