Neculai Fântânaru

Everything Depends On The Leader

Python Parsing: Move Html Links In Another Frame

On Noiembrie 23, 2021
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/1nvCxrKe

Install Python.

Copy the link from the tag: <link rel="canonical".... />

<link rel="canonical" href="https://neculaifantanaru.com/love-running.html" />

Move the link above in the frame < ! -- FLAGS_1 --> to < ! -- FLAGS_2 --> in sections: en, ar, zh, hi, de, ru

<!-- FLAGS_1 -->

<div class="cautareField">
  <div align="right">

  <a href="https://neculaifantanaru.com/stralucirea-nestematei.html">
  <a href="https://neculaifantanaru.com/fr/l-eclat-de-la-gemme.html">
  <a href="https://neculaifantanaru.com/en/brilliance-of-the-gem.html">
  <a href="https://neculaifantanaru.com/es/gema-stargaionss.html">
  <a href="https://neculaifantanaru.com/pt/brilho-da-gema.html">
  <a href="https://neculaifantanaru.com/ar/my-name-is-prince.html">
  <a href="https://neculaifantanaru.com/zh/books-and-magic.html">
  <a href="https://neculaifantanaru.com/hi/many-things.html">
  <a href="https://neculaifantanaru.com/de/horror-scenario.html">
  <a href="https://neculaifantanaru.com/ru/everything-is-here.html">
  
</div>
</div>

<!-- FLAGS_2 -->

THE OUTPUT:

<!-- FLAGS_1 -->

<div class="cautareField">
  <div align="right">

  <a href="https://neculaifantanaru.com/stralucirea-nestematei.html">
  <a href="https://neculaifantanaru.com/fr/l-eclat-de-la-gemme.html">
  <a href="https://neculaifantanaru.com/en/love-running.html">
  <a href="https://neculaifantanaru.com/es/gema-stargaionss.html">
  <a href="https://neculaifantanaru.com/pt/brilho-da-gema.html">
  <a href="https://neculaifantanaru.com/ar/love-running.html">
  <a href="https://neculaifantanaru.com/zh/love-running.html">
  <a href="https://neculaifantanaru.com/hi/love-running.html">
  <a href="https://neculaifantanaru.com/de/love-running.html">
  <a href="https://neculaifantanaru.com/ru/love-running.html">
  
</div>
</div>

<!-- FLAGS_2 -->

The CODE: Copy and run the code below in any interpreter program (I use pyScripter) . Don't forget to change the path on the line: "directory_name =".

import re
import os


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, 'r') 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, 'w') as f:
        f.write(text)

def check_link(file_path):
    text = read_text_from_file(file_path)
    # transformam textul din fisier intr-un string
    text = str(text)
    pattern = re.compile('<link rel="canonical" href="(.*?)" />')
    canonical_link = re.findall(pattern, text)
    if len(canonical_link) != 0:
        file_name = canonical_link[0].split('/')[-1]
        flags_pattern = re.compile('<!-- FLAGS_1 -->[\s\S]*?<!-- FLAGS_2 -->[\s\S]*?')
        text_flags = str(re.findall(flags_pattern, text)[0])
        # print("before: ", text_flags)
        languages = ['en', 'ar', 'zh', 'hi', 'de', 'ru']
        text_flags_new = text_flags
        for language in languages:
            template = re.compile('<a href=\"https://neculaifantanaru.com/{}/(.*?)\">'.format(language))
            links = re.findall(template, text_flags)
            for link in links:
                if link != file_name:
                    text_flags_new = text_flags_new.replace(link, file_name)
        # print("after: ", text_flags_new)
        text = text.replace(text_flags, text_flags_new)
        write_to_file(text, file_path)
    else:
        print("Found a problem with the file: ", file_path)


def check_links_for_all_files(directory_name):
    for file in os.listdir(directory_name):
        filename = str(file)
        print(filename)
        # verificam daca fisierul se termina cu extensia html sau php
        if filename.endswith(".html"): #or filename.endswith(".php"):
            file_path = os.path.join(directory_name, filename)
            # pentru fiecare fisier gasit, stergem spatiile in plus
            check_link(file_path)
        else:
            continue

if __name__ == '__main__':
    check_links_for_all_files("e:\\folder_1")

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.