ro  fr  en  es  pt  ar  zh  hi  de  ru
ART 2.0 ART 3.0 ART 4.0 ART 5.0 ART 6.0 Pinterest

نحوه تولید Sitemaps XML با استفاده از فایل های Python و HTML

On February 16, 2022, in Leadership Quantum-XX, by Neculai Fantanaru

شما می توانید کد کامل را اینجا ببینید:https: // passatin.com / 6GN GP BV6

نصبپیتون.

شما به فایل های زیر نیاز دارید، که می توانید در همان پوشه قرار دهید:

- یک فایل خالی را به نام ایجاد کنیدR. XML(این فایل است که در آن تمام صفحات HTML تولید می شود)

- ایجاد یک فایل به نامfinal_xml.txtکه در آن شما باید این برچسب را در خط اول کپی کنید:

بر گرداندن>

- ایجاد یک فایل به نامstart_xml.txtدر آن شما باید در ابتدا کد زیر را داشته باشید:

نسخه XML="1.0"رمزگذاری="UTF-8">>
که دربر گرداندن
      xmlns: XSI="http: // 呜呜. i 3.org/2001/xml schema-instance"
	  xmlns: XHTML="http: // 呜呜 من 3.org/1999/xhtml"
      xsi: shemalocation="http: // 呜呜. sitemaps.org / schema / sitemap / 0.9 http: // 呜呜 .sitemaps.org / schema / نقشه سایت / 0.9 / نقشه سایت. فاضلاب"
      xmlns="http: // 呜呜 Sitemaps.org / Schemas / Sitemap / 0.9">	  	  
- www.Check-Domains.com نقشه سایت ژنراتور ->

- ایجاد یک فایل به نامmodel_xml.txtدر ابتدا باید کد زیر را داشته باشید:

  
    <محل>https://your-website.com/example - ترس .html
    2021-11-303 17: 19: 37 + 00: 00
    هفتگی
    <اولویت>0.6400
  

کد پایتون (صرفه جویی در هر کجا، فقط مسیر را با پوشه خود را با فایل های 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('(.*?)')
    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()

That's all folks.

اگر کد من را دوست دارید، لطفا آن را به اشتراک بگذارید

این کد را درپاشنهیا کد های پایتوننسخه 3یانسخه 4یانسخه 5

 


Latest articles accessed by readers:

  1. An Eye To See And A Mind To Understand
  2. Turn Towards Me With An Eye Full Of Your Own Gaze
  3. The Snapshot Of Magic In God's Universe
  4. Rhythm Of My Heart

Donate via Paypal

Alternate Text

RECURRENT DONATION

Donate monthly to support
the NeculaiFantanaru.com project

SINGLE DONATION

Donate the desired amount to support
the NeculaiFantanaru.com project

Donate by Bank Transfer

Account Ron: RO34INGB0000999900448439

Open account at ING Bank

Join The Neculai Fantanaru Community



* Note: If you want to read all my articles in real time, please check the romanian version !

decoration
About | Site Map | Partners | Feedback | Terms & Conditions | Privacy | RSS Feeds
© Neculai Fântânaru - All rights reserved