Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Explanation:
- ENGLISH: https://neculaifantanaru.com/en/how-to-generate-sitemaps-xml-using-python-and-html-files.html
- -----------------
- import os
- import re
- import random
- import unidecode
- def read_text_from_file(file_path):
- """
- Aceasta functie returneaza continutul unui fisier.
- file_path: calea catre fisierul din care vrei sa citesti
- """
- with open(file_path, encoding='utf8') as f:
- text = f.read()
- return text
- def write_to_file(text, file_path):
- """
- Aceasta functie scrie un text intr-un fisier.
- text: textul pe care vrei sa il scrii
- file_path: calea catre fisierul in care vrei sa scrii
- """
- with open(file_path, 'wb') as f:
- f.write(text.encode('utf8', 'ignore'))
- def creeaza_fisier_xml():
- lista_nume_fisiere = preia_nume_fisiere_html(
- 'e:\\YOUR-FOLDER-WITH-HTML-FILES',
- # files to ignore
- ['404-1.html', '404-2.html', '404-3.html']
- )
- start_xml = read_text_from_file('start_xml.txt')
- final_xml = read_text_from_file('final_xml.txt')
- model_xml = read_text_from_file('model_xml.txt')
- xml_text = start_xml + '\n'
- link_pattern = re.compile('<loc>(.*?)</loc>')
- link = re.findall(link_pattern, model_xml)
- if len(link) != 0:
- link = link[0]
- for nume_fisier in lista_nume_fisiere:
- model = model_xml
- model = model.replace(link, 'https://YOUR-WEBSITE.com/' + nume_fisier)
- xml_text = xml_text + model + '\n'
- xml_text = xml_text + final_xml
- write_to_file(xml_text, 'rss.xml')
- print("Scriere efectuata cu succes.")
- def preia_nume_fisiere_html(folder_fisiere_html, lista_fisiere_de_ignorat):
- lista_nume_fisiere_html = list()
- for f in os.listdir(folder_fisiere_html):
- if f.endswith('.html'):
- lista_nume_fisiere_html.append(f)
- lista_finala = list()
- for f in lista_nume_fisiere_html:
- if f not in lista_fisiere_de_ignorat:
- lista_finala.append(f)
- return lista_finala
- def main():
- creeaza_fisier_xml()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement