CCCPaste

librus_playwright_wiadomosci

from playwright.sync_api import Playwright, sync_playwright
from bs4 import BeautifulSoup
import pandas as pd

def run(playwright: Playwright) -> None:        
    browser = playwright.firefox.launch(headless=True)          #slow_mo=50
    context = browser.new_context()
     
    # Open new page
    page = context.new_page()
    
    # Go to https://portal.librus.pl/rodzina
    page.goto("https://portal.librus.pl/rodzina")
    
    # Click text=LIBRUS Synergia >> nth=0
    page.locator("text=LIBRUS Synergia").first.click()
    
    # Click nav >> text=Zaloguj >> nth=0
    page.locator("nav >> text=Zaloguj").first.click()
    page.wait_for_url("https://portal.librus.pl/rodzina/synergia/loguj")
    
    # Click input[name="Login"]
    page.frame_locator("#caLoginIframe").locator("input[name=\"Login\"]").click()
    
    # Fill input[name="Login"]
    page.frame_locator("#caLoginIframe").locator("input[name=\"Login\"]").fill("XXXXXXXXXX")       ### login
    
    # Click input[name="Pass"]
    page.frame_locator("#caLoginIframe").locator("input[name=\"Pass\"]").click()
    
    # Fill input[name="Pass"]
    page.frame_locator("#caLoginIframe").locator("input[name=\"Pass\"]").fill("XXXXXXXXXXX")       ### password
    
    # # Click button:has-text("Zaloguj")
    page.frame_locator("#caLoginIframe").locator("button:has-text(\"Zaloguj\")").click()
    page.wait_for_url("https://synergia.librus.pl/rodzic/index")
    page.is_visible('div.container-background')
    
    # # Click #icon-oceny span
    page.locator("#icon-wiadomosci span").click()
    page.wait_for_url("https://synergia.librus.pl/wiadomosci")

    # Select tbody
    html_1 = page.inner_html('xpath=/html/body/div[3]/div[3]/form/div/div/table/tbody/tr/td[2]/table[2]/tbody') 
    soup = BeautifulSoup(html_1, 'html.parser')
    
    list_temp = []    
    for tr in soup.find_all('tr'):
        slownik = []
        for td in tr.find_all('td'):
            if '\n' not in td.text:
                slownik.append(td.text)
            else:
                pass
        list_temp.append(slownik)
   
    mails = []
    for i in list_temp:
        slownik = {}
        slownik = {
            "Autor": i[0],
            "Tytuł": i[1],
            "Data": i[2]
        }
        mails.append(slownik)
    
    df = pd.DataFrame(mails)

    with open('df.json', 'w', encoding='utf-8') as file:
        df.to_json(file, force_ascii=False)    
    
    # ---------------------
    context.close()
    browser.close()
    
with sync_playwright() as playwright:
    run(playwright)