Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +161 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,163 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
+
from urllib.parse import urljoin
|
| 7 |
+
|
| 8 |
+
HEADERS = {"User-Agent": "Mozilla/5.0"}
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# -------------------------------
|
| 12 |
+
# FETCH PAGE
|
| 13 |
+
# -------------------------------
|
| 14 |
+
def fetch_page(url):
|
| 15 |
+
try:
|
| 16 |
+
response = requests.get(url, headers=HEADERS, timeout=10)
|
| 17 |
+
return BeautifulSoup(response.text, "html.parser")
|
| 18 |
+
except:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# -------------------------------
|
| 23 |
+
# EXTRACT FROM BLOCKS (MAIN LOGIC)
|
| 24 |
+
# -------------------------------
|
| 25 |
+
def extract_from_blocks(soup):
|
| 26 |
+
blocks = soup.find_all(["p", "div", "li"])
|
| 27 |
+
data = []
|
| 28 |
+
|
| 29 |
+
for block in blocks:
|
| 30 |
+
text = block.get_text(" ", strip=True)
|
| 31 |
+
|
| 32 |
+
# Email detection
|
| 33 |
+
email_match = re.search(
|
| 34 |
+
r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if email_match:
|
| 38 |
+
email = email_match.group()
|
| 39 |
+
|
| 40 |
+
# Name guess
|
| 41 |
+
words = text.split()
|
| 42 |
+
name = " ".join(words[:3]) if len(words) >= 3 else "Unknown"
|
| 43 |
+
|
| 44 |
+
# Phone
|
| 45 |
+
phone_match = re.search(r"\+?\d[\d\s\-]{8,15}", text)
|
| 46 |
+
phone = phone_match.group() if phone_match else "Not Available"
|
| 47 |
+
|
| 48 |
+
data.append([name, email, phone])
|
| 49 |
+
|
| 50 |
+
return data
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# -------------------------------
|
| 54 |
+
# TRY TO FIND PROFILE LINKS
|
| 55 |
+
# -------------------------------
|
| 56 |
+
def get_profile_links(soup, base_url):
|
| 57 |
+
links = set()
|
| 58 |
+
|
| 59 |
+
for a in soup.find_all("a", href=True):
|
| 60 |
+
href = a["href"]
|
| 61 |
+
full = urljoin(base_url, href)
|
| 62 |
+
|
| 63 |
+
keywords = ["faculty", "profile", "staff", "person", "people"]
|
| 64 |
+
if any(k in full.lower() for k in keywords):
|
| 65 |
+
links.add(full)
|
| 66 |
+
|
| 67 |
+
return list(links)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# -------------------------------
|
| 71 |
+
# EXTRACT FROM PROFILE PAGE
|
| 72 |
+
# -------------------------------
|
| 73 |
+
def extract_from_profile(url):
|
| 74 |
+
soup = fetch_page(url)
|
| 75 |
+
if not soup:
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
text = soup.get_text(" ", strip=True)
|
| 79 |
+
|
| 80 |
+
# Name
|
| 81 |
+
name = "Not Available"
|
| 82 |
+
for tag in ["h1", "h2", "h3"]:
|
| 83 |
+
t = soup.find(tag)
|
| 84 |
+
if t:
|
| 85 |
+
val = t.get_text(strip=True)
|
| 86 |
+
if 2 < len(val) < 80:
|
| 87 |
+
name = val
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
# Email
|
| 91 |
+
email_match = re.search(
|
| 92 |
+
r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", text
|
| 93 |
+
)
|
| 94 |
+
email = email_match.group() if email_match else "Not Available"
|
| 95 |
+
|
| 96 |
+
# Phone
|
| 97 |
+
phone_match = re.search(r"\+?\d[\d\s\-]{8,15}", text)
|
| 98 |
+
phone = phone_match.group() if phone_match else "Not Available"
|
| 99 |
+
|
| 100 |
+
return [name, email, phone]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# -------------------------------
|
| 104 |
+
# MAIN SCRAPER FUNCTION
|
| 105 |
+
# -------------------------------
|
| 106 |
+
def scrape(url):
|
| 107 |
+
soup = fetch_page(url)
|
| 108 |
+
if not soup:
|
| 109 |
+
return None, "β Failed to load page"
|
| 110 |
+
|
| 111 |
+
# Method 1: Direct extraction
|
| 112 |
+
data = extract_from_blocks(soup)
|
| 113 |
+
|
| 114 |
+
# Method 2: Profile-based extraction (fallback)
|
| 115 |
+
if not data:
|
| 116 |
+
links = get_profile_links(soup, url)
|
| 117 |
+
|
| 118 |
+
for link in links[:20]: # limit for speed
|
| 119 |
+
result = extract_from_profile(link)
|
| 120 |
+
if result:
|
| 121 |
+
data.append(result)
|
| 122 |
+
|
| 123 |
+
if not data:
|
| 124 |
+
return None, "β No data found (site may block scraping or use JavaScript)"
|
| 125 |
+
|
| 126 |
+
df = pd.DataFrame(data, columns=["Name", "Email", "Phone"])
|
| 127 |
+
df = df.drop_duplicates()
|
| 128 |
+
|
| 129 |
+
return df, f"β
Found {len(df)} records"
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# -------------------------------
|
| 133 |
+
# STREAMLIT UI
|
| 134 |
+
# -------------------------------
|
| 135 |
+
st.set_page_config(page_title="Faculty Scraper", layout="wide")
|
| 136 |
+
|
| 137 |
+
st.title("π Universal Faculty Scraper")
|
| 138 |
+
st.markdown("Paste a college faculty page URL and extract emails, names, and phone numbers.")
|
| 139 |
+
|
| 140 |
+
url = st.text_input("π Enter Faculty Page URL")
|
| 141 |
+
|
| 142 |
+
if st.button("π Scrape"):
|
| 143 |
+
if not url:
|
| 144 |
+
st.warning("Please enter a URL")
|
| 145 |
+
else:
|
| 146 |
+
with st.spinner("Scraping in progress..."):
|
| 147 |
+
df, msg = scrape(url)
|
| 148 |
+
|
| 149 |
+
if df is not None:
|
| 150 |
+
st.success(msg)
|
| 151 |
+
|
| 152 |
+
st.dataframe(df, use_container_width=True)
|
| 153 |
+
|
| 154 |
+
csv = df.to_csv(index=False).encode("utf-8")
|
| 155 |
|
| 156 |
+
st.download_button(
|
| 157 |
+
label="π₯ Download CSV",
|
| 158 |
+
data=csv,
|
| 159 |
+
file_name="faculty_data.csv",
|
| 160 |
+
mime="text/csv",
|
| 161 |
+
)
|
| 162 |
+
else:
|
| 163 |
+
st.error(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|