Advertisement
jarekmor

piec_playwright

Feb 4th, 2023 (edited)
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. from playwright.sync_api import Playwright, sync_playwright, expect
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import json
  5.  
  6. def run(playwright: Playwright) -> None:
  7.     browser = playwright.chromium.launch(headless=True)
  8.     context = browser.new_context()
  9.  
  10.     page = context.new_page()
  11.  
  12.     page.goto("http://wojszym2244.usermd.net/")
  13.     page.locator("img[alt=\"Zaloguj\"]").click()
  14.  
  15.     page.locator("[placeholder=\"Wprowadź ID wpisane do sterownika\"]").click()
  16.     page.locator("[placeholder=\"Wprowadź ID wpisane do sterownika\"]").fill("1000000000")  # <- to wpisać ID
  17.  
  18.     page.locator("[placeholder=\"Wprowadź Nick zapisany na stronie\"]").click()
  19.     page.locator("[placeholder=\"Wprowadź Nick zapisany na stronie\"]").fill("test") # <- to wpisać nick
  20.  
  21.     page.locator("[placeholder=\"Wprowadź PIN wpisany do sterownika\"]").click()
  22.     page.locator("[placeholder=\"Wprowadź PIN wpisany do sterownika\"]").fill("1234") # <- to wpisać PIN
  23.  
  24.     page.locator("text=Login").click()
  25.    
  26.     # ---------------------
  27.     # od tego miejsca można scrapować np. z wykorzystanem BeautifulSoup
  28.    
  29.     html = page.content()
  30.     soup = BeautifulSoup(html, 'html.parser')
  31.    
  32.     butotalfuel = soup.select('#butotalfuel')
  33.     bu24hfuel = soup.select('#bu24hfuel')
  34.     buactualfuel = soup.select('#buactualfuel')
  35.     OpalStan = soup.select('#OpalStan')
  36.     OpalZostaloDni= soup.select('#OpalZostaloDni')
  37.    
  38.     json_table = {"butotalfuel": float(butotalfuel[0].text),
  39.                   "bu24hfuel": float(bu24hfuel[0].text),
  40.                   "buactualfuel": float(buactualfuel[0].text),
  41.                   "OpalStan": float(OpalStan[0].text[:-2]),
  42.                   "OpalZostaloDni": int(OpalZostaloDni[0].text[:-1])}
  43.          
  44.     json_object = json.dumps(json_table, indent = 4)
  45.      
  46.     # zapis json do pliku piec.json
  47.     with open("piec.json", "w") as outfile:
  48.         outfile.write(json_object)
  49.    
  50.     context.close()
  51.     browser.close()
  52.    
  53. with sync_playwright() as playwright:
  54.     run(playwright)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement