jayanth-2005 commited on
Commit
d861e5a
Β·
verified Β·
1 Parent(s): ec4c7f3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +96 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,98 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
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
+ import urllib3
7
+ import time
8
+
9
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
10
+
11
+ st.set_page_config(page_title="Faculty Scraper", layout="wide")
12
+
13
+ st.title("πŸŽ“ Universal Faculty Scraper")
14
+ st.write("Enter any faculty page URL to extract Name, Email, and Phone.")
15
+
16
+ # -------- INPUT --------
17
+ url = st.text_input("Enter Faculty Page URL")
18
+
19
+ # -------- REGEX --------
20
+ email_regex = r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"
21
+ phone_regex = r"\+?\d[\d\s\-]{8,15}"
22
+
23
+ # -------- SCRAPER FUNCTION --------
24
+ def scrape_faculty(url):
25
+ headers = {"User-Agent": "Mozilla/5.0"}
26
+
27
+ try:
28
+ response = requests.get(url, headers=headers, verify=False, timeout=10)
29
+ except:
30
+ return pd.DataFrame(), "❌ Failed to load page"
31
+
32
+ soup = BeautifulSoup(response.text, "html.parser")
33
+
34
+ faculty_data = []
35
+
36
+ blocks = soup.find_all(["div", "li", "tr", "section"])
37
+
38
+ for block in blocks:
39
+ text = block.get_text(" ", strip=True)
40
+
41
+ if len(text) < 30:
42
+ continue
43
+
44
+ email_match = re.search(email_regex, text)
45
+ email = email_match.group() if email_match else None
46
+
47
+ phone_match = re.search(phone_regex, text)
48
+ phone = phone_match.group() if phone_match else None
49
+
50
+ name = None
51
+ for tag in block.find_all(["h1", "h2", "h3", "h4", "strong", "b"]):
52
+ candidate = tag.get_text(strip=True)
53
+
54
+ if 3 < len(candidate) < 60 and not re.search(email_regex, candidate):
55
+ name = candidate
56
+ break
57
+
58
+ if name or email or phone:
59
+ faculty_data.append([
60
+ name if name else "Not Available",
61
+ email if email else "Not Available",
62
+ phone if phone else "Not Available"
63
+ ])
64
+
65
+ df = pd.DataFrame(faculty_data, columns=["Name", "Email", "Phone"])
66
+ df = df.drop_duplicates()
67
+
68
+ df = df[
69
+ (df["Email"] != "Not Available") |
70
+ (df["Phone"] != "Not Available")
71
+ ]
72
+
73
+ return df, "βœ… Success"
74
+
75
+
76
+ # -------- BUTTON --------
77
+ if st.button("Scrape Data"):
78
+ if url:
79
+ with st.spinner("Scraping in progress..."):
80
+ df, status = scrape_faculty(url)
81
+
82
+ st.success(status)
83
+
84
+ if not df.empty:
85
+ st.dataframe(df)
86
 
87
+ # Download button
88
+ csv = df.to_csv(index=False).encode("utf-8")
89
+ st.download_button(
90
+ "πŸ“₯ Download CSV",
91
+ csv,
92
+ "faculty_data.csv",
93
+ "text/csv"
94
+ )
95
+ else:
96
+ st.warning("No data found. Try another URL.")
97
+ else:
98
+ st.warning("Please enter a URL.")