Neculai Fântânaru

Everything Depends On The Leader

Python: Reading And Saving Text From XPath Element With Selenium

On August 25, 2023
, in
Python Scripts Examples by Neculai Fantanaru

You can view the entire code here: https://pastebin.com/FCw5wGqg

Apart from saving the data to a text file, the code can be used in various ways such as:

Psychological Analysis: You can use extracted text to analyze an individual's feelings by analyzing an article or a blog.

Automatic Translation: You can integrate a translation service to translate the extracted text into another language.

Generating Summaries: You can develop or integrate an algorithm that summarizes the content, useful for providing an overview of a long article.

Indexing and Searching: You can use text to build an indexing and searching system, allowing users to #259; I can't find it specific information quickly.

Content Monitoring: You can use code to monitor content changes on a page. web and receive notifications when the content changes.

Natural Language Processing (NLP): The extracted text can be used as input data for various NLP tasks, such as classification, tagging parts of speech, entity analysis & named etc.

Creating a Database: You can extend the code to extract and structure more information from the site, creating a database of data that can be usedă for analysisă and reporting.

Accessibility: You can use the extracted text to create audio versions of the content, helping visually impaired people to access the information.

In essence, this code serves as a base for many applications that require access to and manipulation of text on the web. Creativity and specific needs will determine how it can best be used.

 

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# DOWNLOAD chromedriver.exe
# https://googlechromelabs.github.io/chrome-for-testing/#stable

driver_path = 'e:/Carte/BB/17 - Site Leadership/alte/Ionel Balauta/Aryeht/Task 1 - Traduce tot site-ul/Doar Google Web/Andreea/Meditatii/2023/Chome/chromedriver.exe'

options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0")
options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(executable_path=driver_path, options=options)

def main():
    try:
        print("Deschiderea paginii web...")
        driver.get('https://neculaifantanaru.com/esenta-operei-de-arta.html')
        time.sleep(5)  # Așteptați ca pagina să se încarce
        print("Pagina web a fost deschisă.")

        xpath = '//*[@id="blog"]/div/div/div[2]/div/div/div/p[2]' # selecteaza elementul din pagina web -> F12 -> Click Dreapta -> Copy -> Copy XPath
        print(f"Căutarea elementului cu XPath-ul: {xpath}")

        # Așteptare explicită pentru un element specific
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, xpath))
        )

        text_data = element.text
        print(f"Text găsit: {text_data}")

        with open("data.txt", "w", encoding="utf-8") as file:
            print("Salvarea datelor în fișierul 'data.txt' ")
            file.write(text_data)
            print("Datele au fost salvate.")

    except Exception as ex:
        print(f"A apărut o eroare: {ex}")
    finally:
        print("Închiderea browserului...")
        driver.close()
        driver.quit()
        print("Browserul a fost închis.")

main()


That's all folks.


Also, see my other Python Scripts ---HERE---

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.