Neculai Fantanaru

Totul depinde de cine conduce

Cum să creezi un Batch Processor cu Python şi Regex pentru înlocuirea tagurilor html (Parsing)

On Iunie 16, 2021
, in
Python Scripts Examples by Neculai Fantanaru YYY

Puteţi vizualiza întregul cod aici: https://pastebin.com/wnuM5Qg5

Un exemplu de cod al paginilor html care vor fi modifcate cu codul PowerShell.Copiaţi textul de mai jops într-un fişier .html, salvaţi în locaţia C:\Folder1

   
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml" dir="ltr" lang="ro">
<head>
<title>YOUR FIRST PAGE</title>
<link rel="canonical" href="https://MY-WEBSITE.COM" />
<meta name="description" content="I LOVE HTML and CSS"/>

<meta name="keywords" content="abordarea frontala a lucrurilor neelucidate"/>
<meta name="abstract" content="My laptop works just fine"/>
<meta name="Subject" content="I think I need a new car."/>
<meta property="og:url" content="https://otherwebsite.com"/>
<meta property="og:title" content="Nobody is here?" />
<meta property="og:description" content="Dance is my passion."/>


<!-- Schema Org Start -->

<script type="application/ld+json">
{
"@context":"https://schema.org",
"@type":"Article",

"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://books-and-reading.com"
},

"headline": "Another glass",
"keywords": "anything, words",
"description": "My name is Prince.",
"image": {
"@type": "ImageObject",
"url": "https://website.com/icon-facebook.jpg"
}

}
</script>

Codul Python de mai jos va copia conţinutul tag-urilor html, în celelalte tag-uri, prin parsing data. Trebuie să aveţi completate doar tag-urile <title> si <meta name="description"... />

import requests
import re

# Path to english folder 1
english_folder1 = r"c:\Folder1"

# Path to english folder 2
english_folder2 = r"c:\Folder1"

extension_file = ".html"

use_parse_folder = True #Face folder nou daca pui True, iar daca pui False redenumeste fisierele in acelasi folder

import os

en1_directory = os.fsencode(english_folder1)
en2_directory = os.fsencode(english_folder2)

print('Going through english folder')
for file in os.listdir(en1_directory):
    filename = os.fsdecode(file)
    print(filename)
    if filename == 'y_key_e479323ce281e459.html' or filename == 'TS_4fg4_tr78.html':
        continue
    if filename.endswith(extension_file):
        with open(os.path.join(english_folder1, filename), encoding='utf-8') as html:
            html = html.read()

            try:
                with open(os.path.join(english_folder2, filename), encoding='utf-8') as en_html:
                    en_html = en_html.read()

                    if False:  # if True: will Parse also the content that starts from <!-- ARTICOL START --> to <!-- ARTICOL FINAL --> and so on
                        try:
                            comment_body = re.search('<!-- ARTICOL START -->.+<!-- ARTICOL FINAL -->', html, flags=re.DOTALL)[0]
                            en_html = re.sub('<!-- ARTICOL START -->.+<!-- ARTICOL FINAL -->', comment_body, en_html, flags=re.DOTALL)
                        except:
                            pass

                        try:
                            comment_body2 = re.search('<!-- FLAGS_1 -->.+<!-- FLAGS -->', html, flags=re.DOTALL)[0]
                            en_html = re.sub('<!-- FLAGS_1 -->.+<!-- FLAGS -->', comment_body2, en_html, flags=re.DOTALL)
                        except:
                            pass

                        try:
                            comment_body3 = re.search('<!-- MENIU BARA SUS -->.+<!-- SFARSIT MENIU BARA SUS -->', html, flags=re.DOTALL)[0]
                            en_html = re.sub('<!-- MENIU BARA SUS -->.+<!-- SFARSIT MENIU BARA SUS -->', comment_body3, en_html, flags=re.DOTALL)
                        except:
                            pass

                    # title to meta
                    try:
                        title = re.search('<title.+/title>', html)[0]
                        title_content = re.search('>(.+)<', title)[1]
                    except:
                        pass

                    try:
                        meta_og_title = re.search('<meta property="og:title".*>', en_html)[0]
                        new_meta_og_title = re.sub(r'content=".+"', f'content="{title_content}"', meta_og_title)
                        en_html = en_html.replace(meta_og_title, new_meta_og_title)
                    except:
                        pass

                    try:
                        meta_keywords = re.search('<meta name="keywords".*>', en_html)[0]
                        new_meta_keywords = re.sub(r'content=".+"', f'content="{title_content}"', meta_keywords)
                        en_html = en_html.replace(meta_keywords, new_meta_keywords)
                    except:
                        pass

                    try:
                        meta_abstract = re.search('<meta name="abstract".*>', en_html)[0]
                        new_meta_abstract = re.sub(r'content=".+"', f'content="{title_content}"', meta_abstract)
                        en_html = en_html.replace(meta_abstract, new_meta_abstract)
                    except:
                        pass

                    try:
                        meta_Subject = re.search('<meta name="Subject".*>', en_html)[0]
                        new_meta_Subject = re.sub(r'content=".+"', f'content="{title_content}"', meta_Subject)
                        en_html = en_html.replace(meta_Subject, new_meta_Subject)
                    except:
                        pass

                    try:
                        headline = re.search('"headline":.+', en_html)[0]
                        new_headline = re.sub(r':.+', f': "{title_content}",', headline)
                        en_html = en_html.replace(headline, new_headline)
                    except:
                        pass

                    try:
                        keywords = re.search('"keywords":.+', en_html)[0]
                        new_keywords = re.sub(r':.+', f': "{title_content}",', keywords)
                        en_html = en_html.replace(keywords, new_keywords)
                    except:
                        pass

                    # canonical to meta og:url and @id
                    try:
                        canonical_content = re.search('<link rel="canonical" href="(.+)".*>', html)[1]
                    except:
                        pass

                    try:
                        og_url = re.search('<meta property="og:url".*>', en_html)[0]
                        new_og_url = re.sub(r'content=".+"', f'content="{canonical_content}"', og_url)
                        en_html = en_html.replace(og_url, new_og_url)
                    except:
                        pass

                    try:
                        id = re.search('"@id":.+', en_html)[0]
                        new_id = re.sub(r':.+', f': "{canonical_content}"', id)
                        en_html = en_html.replace(id, new_id)
                    except:
                        pass

                    # meta description to og:description and description
                    try:
                        meta = re.search('<meta name="description".+/>', html)[0]
                        meta_description = re.search('<meta name="description" content="(.+)".+>', html)[1]
                    except:
                        pass

                    try:
                        og_description = re.search('<meta property="og:description".+/>', en_html)[0]
                        new_og_description = re.sub(r'content=".+"', f'content="{meta_description}"', og_description)
                        en_html = en_html.replace(og_description, new_og_description)
                    except:
                        pass

                    try:
                        description = re.search('"description":.+', en_html)[0]
                        new_description = re.sub(r':.+', f': "{meta_description}",', description)
                        en_html = en_html.replace(description, new_description)
                    except:
                        pass

                    try:
                        en_html = re.sub('<meta name="description".+/>', meta, en_html)
                    except:
                        pass

                    try:
                        en_html = re.sub('<title.+/title>', title, en_html)
                    except:
                        pass
            except FileNotFoundError:
                continue

        print(f'{filename} parsed')
        if use_parse_folder:
            try:
                with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html:
                    new_html.write(en_html)
            except:
                os.mkdir(english_folder2+r'\parsed')
                with open(os.path.join(english_folder2+r'\parsed', 'parsed_'+filename), 'w', encoding='utf-8') as new_html:
                    new_html.write(en_html)
        else:
            with open(os.path.join(english_folder2, 'parsed_'+filename), 'w', encoding='utf-8') as html:
                html.write(en_html)

Opţional. Iată o expresie REGEX care va modifica tag-ul "KEYWORDS" din pagina html, adăugând virgulă după fiecare cuvânt.

Use with Notepad++ -> Ctr+F -> Check: Regular Expression

SEARCH: (?s)<title>.*?<\/title>.*?<meta\x20name="keywords"\x20content="\K(\w+)|\G[^\w\r\n]+(\w+)  
REPLACE BY:  ?1\l\1:,\x20\l\2

Puteţi încerca această variantă complexă de cod care, face mai mult: preia datele din tagul <title> şi le copiază în tagul <meta name="keywords" content=" "/> Puteţi vedea codul aici:

https://pastebin.com/jM5zf2qS

import requests
import re

# Path to english folder 1

english_folder2 = r"c:\Folder1"

extension_file = ".html"

use_parse_folder = True

import os

en1_directory = os.fsencode(english_folder2)
en2_directory = os.fsencode(english_folder2)

# These connection words will be ignore when parsing data from <title> tag to <meta keywords> tag
LISTA_CUVINTE_LEGATURA = [
    'in', 'la', 'unei', 'si', 'sa', 'se', 'de', 'prin', 'unde', 'care', 'a',
    'al', 'prea', 'lui', 'din', 'ai', 'unui', 'acei', 'un', 'doar', 'tine',
    'ale', 'sau', 'dintre', 'intre', 'cu','ce', 'va', 'fi', 'este', 'cand', 'o',
    'cine', 'aceasta', 'ca', 'dar', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII',
    'to', 'was', 'your', 'you', 'is', 'are', 'iar', 'fara', 'aceasta', 'pe', 'tu',
    'nu', 'mai', 'ne', 'le', 'intr', 'cum', 'e', 'for', 'she', 'it', 'esti',
	'this', 'that', 'how', 'can', 't', 'must', 'be', 'the', 'and', 'do', 'so', 'or', 'ori',
	'who', 'what', 'if', 'of', 'on', 'i', 'we', 'they', 'them', 'but', 'where', 'by', 'an',
	'on', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'made', 'make', 'my', 'me', '-',
	'vom', 'voi', 'ei', 'cat', 'ar', 'putea', 'poti', 'sunteti', 'inca', 'still', 'noi', 'l',
	'ma', 's', 'dupa', 'after', 'under', 'sub', 'niste', 'some', 'those', 'he'
]

def creeaza_lista_keywords(titlu):
    # imparte titlul in 2 in functie de bara verticala |
    prima_parte_titlu = titlu.split('|')[0]
    # extrage toate cuvintele din prima parte a titlului
    keywords = re.findall(r'(?:\w|-*\!)+', prima_parte_titlu)
    # extrage keyword-urile care nu se gasesc in lista de cuvinte de legatura
    keywords_OK = list()
    for keyword in keywords:
        if keyword not in LISTA_CUVINTE_LEGATURA:
            # adauga keyword-ul cu litere mici
            keywords_OK.append(keyword.lower())
    # returneaza un string in care toate keyword-urile sunt alaturate prin ', '
    return ", ".join(keywords_OK)


print('Going through english folder')
amount = 1
for file in os.listdir(en1_directory):
    filename = os.fsdecode(file)
    print(filename)
    if filename == 'y_key_e479323ce281e459.html' or filename == 'directory.html':
        continue
    if filename.endswith(extension_file):
        with open(os.path.join(english_folder2, filename), encoding='utf-8') as html:
            html = html.read()

            try:
                with open(os.path.join(english_folder2, filename), encoding='utf-8') as en_html:
                    en_html = en_html.read()

                    # title to meta
                    try:
                        title = re.search('<title.+/title>', html)[0]
                        title_content = re.search('>(.+)<', title)[1]
                    except:
                        pass

                    try:
                        meta_og_title = re.search('<meta property="og:title".*>', en_html)[0]
                        new_meta_og_title = re.sub(r'content=".+"', f'content="{title_content}"', meta_og_title)
                        en_html = en_html.replace(meta_og_title, new_meta_og_title)
                    except:
                        pass

                    try:
                        meta_keywords = re.search('<meta name="keywords".*>', en_html)[0]
                        keywords = creeaza_lista_keywords(title_content)
                        new_meta_keywords = re.sub(r'content=".+"', f'content="{keywords}"', meta_keywords)
                        en_html = en_html.replace(meta_keywords, new_meta_keywords)
                    except:
                        pass

                    try:
                        meta_abstract = re.search('<meta name="abstract".*>', en_html)[0]
                        new_meta_abstract = re.sub(r'content=".+"', f'content="{title_content}"', meta_abstract)
                        en_html = en_html.replace(meta_abstract, new_meta_abstract)
                    except:
                        pass

                    try:
                        meta_Subject = re.search('<meta name="Subject".*>', en_html)[0]
                        new_meta_Subject = re.sub(r'content=".+"', f'content="{title_content}"', meta_Subject)
                        en_html = en_html.replace(meta_Subject, new_meta_Subject)
                    except:
                        pass

                    try:
                        headline = re.search('"headline":.+', en_html)[0]
                        new_headline = re.sub(r':.+', f': "{title_content}",', headline)
                        en_html = en_html.replace(headline, new_headline)
                    except:
                        pass

                    try:
                        keywords = re.search('"keywords":.+', en_html)[0]
                        new_keywords = re.sub(r':.+', f': "{title_content}",', keywords)
                        en_html = en_html.replace(keywords, new_keywords)
                    except:
                        pass

                    # canonical to meta og:url and @id
                    try:
                        canonical_content = re.search('<link rel="canonical" href="(.+)".*>', html)[1]
                    except:
                        pass

                    try:
                        og_url = re.search('<meta property="og:url".*>', en_html)[0]
                        new_og_url = re.sub(r'content=".+"', f'content="{canonical_content}"', og_url)
                        en_html = en_html.replace(og_url, new_og_url)
                    except:
                        pass

                    try:
                        id = re.search('"@id":.+', en_html)[0]
                        new_id = re.sub(r':.+', f': "{canonical_content}"', id)
                        en_html = en_html.replace(id, new_id)
                    except:
                        pass

                    # meta description to og:description and description
                    try:
                        meta = re.search('<meta name="description".+>', html)[0]
                        meta_description = re.search('<meta name="description" content="(.+)".*>', html)[1]
                    except:
                        pass

                    try:
                        og_description = re.search('<meta property="og:description".+/>', en_html)[0]
                        new_og_description = re.sub(r'content=".+"', f'content="{meta_description}"', og_description)
                        en_html = en_html.replace(og_description, new_og_description)
                    except:
                        pass

                    try:
                        description = re.search('"description":.+', en_html)[0]
                        new_description = re.sub(r':.+', f': "{meta_description}",', description)
                        en_html = en_html.replace(description, new_description)
                    except:
                        pass

                    try:
                        en_html = re.sub('<meta name="description".+/>', meta, en_html)
                    except:
                        pass

                    try:
                        en_html = re.sub('<title.+/title>', title, en_html)
                    except:
                        pass
            except FileNotFoundError:
                continue

        print(f'{filename} parsed ({amount})')
        amount += 1
        if use_parse_folder:
            try:
                with open(os.path.join(english_folder2+r'', ''+filename), 'w', encoding='utf-8') as new_html:
                    new_html.write(en_html)
            except:
                os.mkdir(english_folder2+r'')
                with open(os.path.join(english_folder2+r'', ''+filename), 'w', encoding='utf-8') as new_html:
                    new_html.write(en_html)
        else:
            with open(os.path.join(english_folder2, 'parsed_'+filename), 'w', encoding='utf-8') as html:
                html.write(en_html)

That's all folks.

If you like my code, please SHARE IT

Puteţi vizualiza şi versiunea de cod în PowerShell. Also, you can see other Python Codes: VERSION 2 of this code. Or Version 3 OR Version 4 OR Version 5

Alatura-te Comunitatii Neculai Fantanaru
Cele 63 de calităţi ale liderului
Cele 63 de calităţi ale liderului

De ce să citeşti această carte? Pentru că este hotărâtoare pentru optimizarea performanţelor tale. Fiindcă pune accent mai mult pe latura umană decât pe conceptul de business, ceea ce permite cu uşurinţă citirea şi înţelegerea ei.

Leadership - Magia măiestriei
Leadership - Magia măiestriei

Trăsătura esenţială a acestei cărţi, faţă de altele existente pe piaţă din acelaşi domeniu, este aceea că descrie, prin exemple, competenţele ideale ale unui lider. N-am susţinut niciodată că eşte uşor să devii un lider foarte bun, dar dacă veţi urma pas cu pas...

Atingerea maestrului
Atingerea maestrului

Pentru unii lideri „a conduce” înseamnă mai mult a juca un joc de şah, un joc de inteligenţă şi perspicacitate; pentru alţii un joc de noroc, un joc pe care cred că-l pot câştiga mergând de fiecare dată la risc şi pariind totul pe o singură carte.

Leadership Puzzle
Leadership Puzzle

Am scris această carte, care combină într-un mod simplu dezvoltarea personală cu leadershipul, ca pe un joc de puzzle, unde trebuie să combinaţi toate piesele date pentru a reconstitui imaginea de ansamblu.

Performanţa în conducere
Leadership - Pe înţelesul tuturor

Scopul acestei cărţi este de a vă oferi cât mai multe informaţii preţioase prin exemple concrete, şi de a vă arăta o cale prin care să dobândiţi capacitatea de a-i determina pe ceilalţi să vadă lucrurile din aceeaşi perspectivă ca dumneavoastră.

Leadership - Pe înţelesul tuturor
Leadership - Pe înţelesul tuturor

Urmăresc în rândurile acestei cărţi să trezesc interesul omului obişnuit pentru acţiune şi succes. Mesajul acestui volum este că o naţiune puternică este format din oameni puternici şi de succes. Iar fiecare din noi are potenţial, deci succes…