Update extract.py
Browse files- extract.py +25 -24
extract.py
CHANGED
@@ -8,7 +8,6 @@ import time
|
|
8 |
from selenium.webdriver.common.by import By
|
9 |
from selenium.webdriver.support.ui import WebDriverWait
|
10 |
from selenium.webdriver.support import expected_conditions as EC
|
11 |
-
from bs4 import BeautifulSoup
|
12 |
|
13 |
def take_webdata(url):
|
14 |
options = webdriver.ChromeOptions()
|
@@ -34,34 +33,36 @@ def take_webdata(url):
|
|
34 |
return Image.open(BytesIO(screenshot)) , page_title
|
35 |
|
36 |
|
37 |
-
def scrape_vehicle(
|
38 |
-
soup = BeautifulSoup(page_source, "html.parser")
|
39 |
data_kendaraan = {}
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
rincians = []
|
49 |
-
|
50 |
-
|
51 |
-
rows =
|
52 |
-
for row in rows[1:]: #
|
53 |
-
cols = row.
|
54 |
if len(cols) >= 3:
|
55 |
rincian = {
|
56 |
-
"pokok": cols[0].
|
57 |
-
"denda": cols[1].
|
58 |
-
"total": cols[2].
|
59 |
}
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
return data_kendaraan, rincians
|
67 |
|
@@ -102,7 +103,7 @@ def get_vehicle_info(plate_number: str):
|
|
102 |
driver.set_window_size(1920, scroll_height + 200) # force full-page height
|
103 |
time.sleep(1)
|
104 |
|
105 |
-
data_kendaraan, rincian = scrape_vehicle(driver
|
106 |
|
107 |
print(data_kendaraan, rincian)
|
108 |
|
|
|
8 |
from selenium.webdriver.common.by import By
|
9 |
from selenium.webdriver.support.ui import WebDriverWait
|
10 |
from selenium.webdriver.support import expected_conditions as EC
|
|
|
11 |
|
12 |
def take_webdata(url):
|
13 |
options = webdriver.ChromeOptions()
|
|
|
33 |
return Image.open(BytesIO(screenshot)) , page_title
|
34 |
|
35 |
|
36 |
+
def scrape_vehicle(driver):
|
|
|
37 |
data_kendaraan = {}
|
38 |
+
try:
|
39 |
+
rows = driver.find_elements(By.CSS_SELECTOR, "table tr")
|
40 |
+
for row in rows:
|
41 |
+
cols = row.find_elements(By.TAG_NAME, "td")
|
42 |
+
if len(cols) >= 3:
|
43 |
+
key = cols[0].text.strip().lower().replace(".", "").replace(" ", "_")
|
44 |
+
value = cols[2].text.strip()
|
45 |
+
data_kendaraan[key] = value
|
46 |
+
except Exception as e:
|
47 |
+
print("Gagal parsing tabel:", e)
|
48 |
|
49 |
rincians = []
|
50 |
+
try:
|
51 |
+
container = driver.find_element(By.ID, "det_pkb")
|
52 |
+
rows = container.find_elements(By.CLASS_NAME, "row")
|
53 |
+
for row in rows[1:]: # skip header
|
54 |
+
cols = row.find_elements(By.TAG_NAME, "p")
|
55 |
if len(cols) >= 3:
|
56 |
rincian = {
|
57 |
+
"pokok": cols[0].text.strip(),
|
58 |
+
"denda": cols[1].text.strip(),
|
59 |
+
"total": cols[2].text.strip(),
|
60 |
}
|
61 |
+
if len(cols) > 3:
|
62 |
+
rincian["jenis"] = cols[3].text.strip().upper()
|
63 |
+
rincians.append(rincian)
|
64 |
+
except Exception as e:
|
65 |
+
print("Gagal parsing det_pkb:", e)
|
66 |
|
67 |
return data_kendaraan, rincians
|
68 |
|
|
|
103 |
driver.set_window_size(1920, scroll_height + 200) # force full-page height
|
104 |
time.sleep(1)
|
105 |
|
106 |
+
data_kendaraan, rincian = scrape_vehicle(driver)
|
107 |
|
108 |
print(data_kendaraan, rincian)
|
109 |
|