Update src/streamlit_app.py
Browse files- src/streamlit_app.py +49 -75
src/streamlit_app.py
CHANGED
|
@@ -1,99 +1,73 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from selenium import webdriver
|
| 3 |
from selenium.webdriver.chrome.service import Service
|
| 4 |
from selenium.webdriver.chrome.options import Options
|
| 5 |
from webdriver_manager.chrome import ChromeDriverManager
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
-
import os
|
|
|
|
| 8 |
|
| 9 |
st.set_page_config(page_title="Klasemen Liga 1 - Flashscore", layout="wide")
|
| 10 |
-
|
| 11 |
st.title("🏆 Klasemen Liga 1 Indonesia (Flashscore)")
|
| 12 |
-
st.write("Data
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
os.environ['WDM_LOCAL'] = '1'
|
| 20 |
os.environ['WDM_LOG_LEVEL'] = '0'
|
| 21 |
os.environ['WDM_CACHE_DIR'] = '/tmp/.wdm'
|
| 22 |
|
| 23 |
chrome_options = Options()
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
chrome_options.add_argument("--disable-gpu")
|
| 26 |
chrome_options.add_argument("--no-sandbox")
|
| 27 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 28 |
chrome_options.add_argument("--window-size=1920,1080")
|
| 29 |
-
chrome_options.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
st.success("Berhasil mengambil tabel klasemen ✅")
|
| 48 |
-
|
| 49 |
-
responsive_html = f"""
|
| 50 |
-
<html>
|
| 51 |
-
<head>
|
| 52 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 53 |
-
<style>
|
| 54 |
-
body {{
|
| 55 |
-
font-family: Arial, sans-serif;
|
| 56 |
-
margin: 0;
|
| 57 |
-
padding: 0;
|
| 58 |
-
background: #f8f9fa;
|
| 59 |
-
}}
|
| 60 |
-
.table-container {{
|
| 61 |
-
max-width: 100%;
|
| 62 |
-
overflow-x: auto;
|
| 63 |
-
background: white;
|
| 64 |
-
margin: 20px auto;
|
| 65 |
-
padding: 10px;
|
| 66 |
-
border-radius: 10px;
|
| 67 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 68 |
-
}}
|
| 69 |
-
.ui-table, table {{
|
| 70 |
-
width: 100%;
|
| 71 |
-
border-collapse: collapse;
|
| 72 |
-
}}
|
| 73 |
-
.ui-table tr:nth-child(even) {{
|
| 74 |
-
background: #f2f2f2;
|
| 75 |
-
}}
|
| 76 |
-
.ui-table th, .ui-table td {{
|
| 77 |
-
padding: 8px 10px;
|
| 78 |
-
text-align: left;
|
| 79 |
-
font-size: 14px;
|
| 80 |
-
}}
|
| 81 |
-
@media (max-width: 600px) {{
|
| 82 |
-
.ui-table th, .ui-table td {{
|
| 83 |
-
font-size: 12px;
|
| 84 |
-
padding: 6px;
|
| 85 |
-
}}
|
| 86 |
-
}}
|
| 87 |
-
</style>
|
| 88 |
-
</head>
|
| 89 |
-
<body>
|
| 90 |
-
<div class="table-container">
|
| 91 |
-
{str(table_div)}
|
| 92 |
-
</div>
|
| 93 |
-
</body>
|
| 94 |
-
</html>
|
| 95 |
-
"""
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
st.error("Gagal menemukan tabel klasemen. Coba tingkatkan waktu tunggu atau periksa struktur halaman.")
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
from selenium import webdriver
|
| 4 |
from selenium.webdriver.chrome.service import Service
|
| 5 |
from selenium.webdriver.chrome.options import Options
|
| 6 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 7 |
+
from selenium.webdriver.common.by import By
|
| 8 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
| 9 |
+
from selenium.webdriver.support import expected_conditions as EC
|
| 10 |
+
from selenium.common.exceptions import TimeoutException, WebDriverException
|
| 11 |
from bs4 import BeautifulSoup
|
| 12 |
+
import os
|
| 13 |
+
import time
|
| 14 |
|
| 15 |
st.set_page_config(page_title="Klasemen Liga 1 - Flashscore", layout="wide")
|
|
|
|
| 16 |
st.title("🏆 Klasemen Liga 1 Indonesia (Flashscore)")
|
| 17 |
+
st.write("Data diambil menggunakan headless browser (Selenium) dan ditampilkan ulang dalam HTML responsif.")
|
| 18 |
|
| 19 |
+
URL = "https://www.flashscore.co.id/sepak-bola/indonesia/super-league/peringkat/#/QqIn9e16/peringkat/keseluruhan/"
|
| 20 |
|
| 21 |
+
def find_chrome_binary():
|
| 22 |
+
# Daftar lokasi yang umum di Spaces / container linux
|
| 23 |
+
candidates = [
|
| 24 |
+
"/usr/bin/google-chrome",
|
| 25 |
+
"/usr/bin/google-chrome-stable",
|
| 26 |
+
"/usr/bin/chromium-browser",
|
| 27 |
+
"/usr/bin/chromium",
|
| 28 |
+
"/snap/bin/chromium",
|
| 29 |
+
]
|
| 30 |
+
for p in candidates:
|
| 31 |
+
if os.path.exists(p):
|
| 32 |
+
return p
|
| 33 |
+
return None
|
| 34 |
|
| 35 |
+
def get_driver(timeout=30):
|
| 36 |
+
# Pastikan webdriver-manager menulis cache di folder yang bisa ditulis
|
| 37 |
os.environ['WDM_LOCAL'] = '1'
|
| 38 |
os.environ['WDM_LOG_LEVEL'] = '0'
|
| 39 |
os.environ['WDM_CACHE_DIR'] = '/tmp/.wdm'
|
| 40 |
|
| 41 |
chrome_options = Options()
|
| 42 |
+
# headless baru jika tersedia; fallback ke --headless jika diperlukan
|
| 43 |
+
try:
|
| 44 |
+
chrome_options.add_argument("--headless=new")
|
| 45 |
+
except Exception:
|
| 46 |
+
chrome_options.add_argument("--headless")
|
| 47 |
chrome_options.add_argument("--disable-gpu")
|
| 48 |
chrome_options.add_argument("--no-sandbox")
|
| 49 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 50 |
chrome_options.add_argument("--window-size=1920,1080")
|
| 51 |
+
chrome_options.add_argument("--disable-extensions")
|
| 52 |
+
chrome_options.add_argument("--disable-background-networking")
|
| 53 |
+
chrome_options.add_argument("--disable-sync")
|
| 54 |
+
chrome_options.add_argument("--metrics-recording-only")
|
| 55 |
+
chrome_options.add_argument("--disable-default-apps")
|
| 56 |
+
chrome_options.add_argument("--mute-audio")
|
| 57 |
+
chrome_options.add_argument("--hide-scrollbars")
|
| 58 |
+
chrome_options.add_argument("--disable-software-rasterizer")
|
| 59 |
+
chrome_options.add_argument("--remote-debugging-port=9222")
|
| 60 |
|
| 61 |
+
chrome_bin = find_chrome_binary()
|
| 62 |
+
if chrome_bin:
|
| 63 |
+
chrome_options.binary_location = chrome_bin
|
| 64 |
|
| 65 |
+
# install driver (tanpa argumen path untuk kompatibilitas versi)
|
| 66 |
+
driver_path = ChromeDriverManager().install()
|
| 67 |
+
service = Service(driver_path)
|
| 68 |
+
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 69 |
+
driver.set_page_load_timeout(timeout)
|
| 70 |
+
return driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
def extract_table_html(page_source):
|
| 73 |
+
soup = BeautifulSo
|
|
|