Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,34 +6,32 @@ import gradio as gr
|
|
| 6 |
|
| 7 |
|
| 8 |
# ---------------------------------
|
| 9 |
-
# Extract
|
| 10 |
# ---------------------------------
|
| 11 |
-
def
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
if
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
lines = text.split("\n")
|
| 30 |
-
|
| 31 |
-
for line in lines:
|
| 32 |
-
if 2 <= len(line.split()) <= 5 and "@" not in line:
|
| 33 |
-
if not any(word in line.lower() for word in [
|
| 34 |
-
"department", "school", "university", "iit"
|
| 35 |
-
]):
|
| 36 |
-
return line
|
| 37 |
|
| 38 |
return "Not found"
|
| 39 |
|
|
@@ -65,17 +63,20 @@ def get_professor_details(college_url):
|
|
| 65 |
res = requests.get(page, headers=headers, timeout=10)
|
| 66 |
page_soup = BeautifulSoup(res.text, "html.parser")
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
for
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
except:
|
| 81 |
continue
|
|
@@ -110,8 +111,8 @@ iface = gr.Interface(
|
|
| 110 |
fn=get_professor_details,
|
| 111 |
inputs=gr.Textbox(label="Enter College Website URL"),
|
| 112 |
outputs=gr.Textbox(label="Professor Details"),
|
| 113 |
-
title="🎓 Professor Scraper (
|
| 114 |
-
description="Extracts
|
| 115 |
)
|
| 116 |
|
| 117 |
iface.launch()
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
# ---------------------------------
|
| 9 |
+
# Extract name from same block
|
| 10 |
# ---------------------------------
|
| 11 |
+
def extract_name(tag):
|
| 12 |
+
# Step 1: check headings inside same block
|
| 13 |
+
for h in tag.find_all(["h1", "h2", "h3", "h4", "strong", "b"]):
|
| 14 |
+
name = h.get_text(strip=True)
|
| 15 |
+
if 2 <= len(name.split()) <= 5:
|
| 16 |
+
return name
|
| 17 |
+
|
| 18 |
+
# Step 2: fallback to nearby text
|
| 19 |
+
text = tag.get_text("\n", strip=True)
|
| 20 |
+
lines = text.split("\n")
|
| 21 |
|
| 22 |
+
for line in lines:
|
| 23 |
+
line = line.strip()
|
| 24 |
|
| 25 |
+
if "@" in line:
|
| 26 |
+
continue
|
| 27 |
|
| 28 |
+
if any(word in line.lower() for word in [
|
| 29 |
+
"department", "school", "iit", "university"
|
| 30 |
+
]):
|
| 31 |
+
continue
|
| 32 |
|
| 33 |
+
if 2 <= len(line.split()) <= 5:
|
| 34 |
+
return line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
return "Not found"
|
| 37 |
|
|
|
|
| 63 |
res = requests.get(page, headers=headers, timeout=10)
|
| 64 |
page_soup = BeautifulSoup(res.text, "html.parser")
|
| 65 |
|
| 66 |
+
# 🔥 KEY: scan structured blocks
|
| 67 |
+
for tag in page_soup.find_all(["div", "li", "td"]):
|
| 68 |
+
text = tag.get_text(" ", strip=True)
|
| 69 |
+
|
| 70 |
+
emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)
|
| 71 |
|
| 72 |
+
if emails:
|
| 73 |
+
name = extract_name(tag)
|
| 74 |
|
| 75 |
+
for email in emails:
|
| 76 |
+
results.append({
|
| 77 |
+
"name": name,
|
| 78 |
+
"email": email
|
| 79 |
+
})
|
| 80 |
|
| 81 |
except:
|
| 82 |
continue
|
|
|
|
| 111 |
fn=get_professor_details,
|
| 112 |
inputs=gr.Textbox(label="Enter College Website URL"),
|
| 113 |
outputs=gr.Textbox(label="Professor Details"),
|
| 114 |
+
title="🎓 Professor Scraper (Final Working)",
|
| 115 |
+
description="Extracts professor names and emails from college websites"
|
| 116 |
)
|
| 117 |
|
| 118 |
iface.launch()
|