Skip to content

Instantly share code, notes, and snippets.

@arbaes
Created March 20, 2019 14:44
Show Gist options
  • Select an option

  • Save arbaes/5fac4ab6b8f2560552063db3a2ff725c to your computer and use it in GitHub Desktop.

Select an option

Save arbaes/5fac4ab6b8f2560552063db3a2ff725c to your computer and use it in GitHub Desktop.
IESN - Webpage Improver
import requests
# Si vous vouliez ignorer la casse, vous devrez utiliser la libriaire 're'.
# 're' est le module python qui permet d'utiliser des regex (Regular Expressions)
# import re
TARGET_URL = "https://www.sap.com/belgique/index.html";
def odoofy(url, word_to_replace):
r = requests.get(url)
# word_to_replace_insensitive = re.compile(re.escape(word_to_replace), re.IGNORECASE)
if r.status_code == 200:
page_content = r.text
improved_page_content = page_content.replace('SAP', 'Odoo')
# word_to_replace_insensitive.sub('Odoo', page_content)
# Ecriture dans un fichier
improved_index_page = open('improved_index_page.html', 'w+')
improved_index_page.write(improved_page_content)
improved_index_page.close()
else:
print("Impossible de contacter l'hote")
odoofy(TARGET_URL, 'SAP')
@bouvyd

bouvyd commented Mar 20, 2019

Copy link
Copy Markdown

pour l'écriture dans le fichier, tu peux utiliser un contexte avec le with, c'est plus pythonique en général

with open('myfile.html', 'w+') as f:
    f.write(content)

pas besoin de close, il est appelé automatiquement quand tu sors du contexte

cela dit, pour un tuto pour des 2ème c'est peut-être perturbant...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment