Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -53
src/streamlit_app.py
CHANGED
|
@@ -1,76 +1,46 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from selenium import webdriver
|
|
|
|
| 3 |
from selenium.webdriver.chrome.options import Options
|
| 4 |
from webdriver_manager.chrome import ChromeDriverManager
|
| 5 |
import time
|
| 6 |
-
import smtplib
|
| 7 |
-
from email.mime.text import MIMEText
|
| 8 |
-
from email.mime.multipart import MIMEMultipart
|
| 9 |
-
from email.mime.application import MIMEApplication
|
| 10 |
-
import os
|
| 11 |
|
| 12 |
-
st.set_page_config(page_title="
|
| 13 |
|
| 14 |
-
st.title("🏆
|
| 15 |
-
st.
|
| 16 |
|
| 17 |
-
|
| 18 |
-
email_user = st.text_input("Masukkan alamat email tujuan:")
|
| 19 |
-
start_button = st.button("Mulai Scraping")
|
| 20 |
|
| 21 |
-
if
|
| 22 |
-
|
| 23 |
-
st.error("❌ Harap isi alamat email terlebih dahulu!")
|
| 24 |
-
else:
|
| 25 |
-
st.info("🚀 Menjalankan headless browser... harap tunggu 10-15 detik.")
|
| 26 |
|
| 27 |
-
|
| 28 |
chrome_options = Options()
|
| 29 |
-
chrome_options.add_argument("--headless
|
| 30 |
chrome_options.add_argument("--no-sandbox")
|
| 31 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
|
| 34 |
-
|
| 35 |
-
url = "https://www.flashscore.co.id/sepak-bola/indonesia/super-league/peringkat/#/QqIn9e16/peringkat/keseluruhan/"
|
| 36 |
driver.get(url)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
time.sleep(
|
| 40 |
|
| 41 |
-
# Ambil HTML penuh
|
| 42 |
page_source = driver.page_source
|
| 43 |
driver.quit()
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
f.write(page_source)
|
| 49 |
-
|
| 50 |
-
st.success("✅ Halaman berhasil diambil dan disimpan ke file `flashscore_klasemen.txt`")
|
| 51 |
-
|
| 52 |
-
# === Kirim email ===
|
| 53 |
-
sender_email = "YOUR_EMAIL@gmail.com"
|
| 54 |
-
sender_password = "YOUR_APP_PASSWORD"
|
| 55 |
-
|
| 56 |
-
msg = MIMEMultipart()
|
| 57 |
-
msg["From"] = sender_email
|
| 58 |
-
msg["To"] = email_user
|
| 59 |
-
msg["Subject"] = "Hasil Scraping Klasemen Flashscore"
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
part['Content-Disposition'] = f'attachment; filename="{filename}"'
|
| 67 |
-
msg.attach(part)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
server.login(sender_email, sender_password)
|
| 72 |
-
server.send_message(msg)
|
| 73 |
-
server.quit()
|
| 74 |
-
st.success(f"📩 File berhasil dikirim ke {email_user}")
|
| 75 |
-
except Exception as e:
|
| 76 |
-
st.error(f"❌ Gagal mengirim email: {e}")
|
|
|
|
| 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 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
st.set_page_config(page_title="Klasemen Liga 1 - Flashscore", layout="wide")
|
| 9 |
|
| 10 |
+
st.title("🏆 Klasemen Liga 1 Indonesia (via Flashscore)")
|
| 11 |
+
st.markdown("Data diambil secara langsung menggunakan **headless browser (Selenium)**.")
|
| 12 |
|
| 13 |
+
url = "https://www.flashscore.co.id/sepak-bola/indonesia/super-league/peringkat/#/QqIn9e16/peringkat/keseluruhan/"
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
if st.button("🔄 Ambil Data Klasemen"):
|
| 16 |
+
st.info("Sedang memuat halaman Flashscore... harap tunggu 5–10 detik ⏳")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
try:
|
| 19 |
chrome_options = Options()
|
| 20 |
+
chrome_options.add_argument("--headless")
|
| 21 |
chrome_options.add_argument("--no-sandbox")
|
| 22 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 23 |
+
chrome_options.add_argument("--window-size=1920,1080")
|
| 24 |
+
chrome_options.add_argument("--disable-gpu")
|
| 25 |
|
| 26 |
+
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
|
|
|
|
|
|
|
| 27 |
driver.get(url)
|
| 28 |
|
| 29 |
+
# tunggu JavaScript selesai
|
| 30 |
+
time.sleep(8)
|
| 31 |
|
|
|
|
| 32 |
page_source = driver.page_source
|
| 33 |
driver.quit()
|
| 34 |
|
| 35 |
+
# tampilkan hasil HTML
|
| 36 |
+
with st.expander("Lihat Source HTML (klik untuk membuka)"):
|
| 37 |
+
st.code(page_source[:3000] + "...", language="html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# tampilkan langsung di browser
|
| 40 |
+
st.components.v1.html(page_source, height=900, scrolling=True)
|
| 41 |
|
| 42 |
+
except Exception as e:
|
| 43 |
+
st.error(f"Terjadi kesalahan: {e}")
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
else:
|
| 46 |
+
st.write("Tekan tombol di atas untuk memuat data klasemen.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|