jayanth-2005 commited on
Commit
d1ccabd
·
verified ·
1 Parent(s): 0364150

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -6,34 +6,32 @@ import gradio as gr
6
 
7
 
8
  # ---------------------------------
9
- # Extract closest name near email
10
  # ---------------------------------
11
- def find_name_near_email(tag):
12
- # Look upward in same block
13
- parent = tag.find_parent()
14
-
15
- if not parent:
16
- return "Not found"
 
 
 
 
17
 
18
- # Check inside same block
19
- candidates = parent.find_all(["h1", "h2", "h3", "h4", "strong", "b"])
20
 
21
- for c in candidates:
22
- text = c.get_text(strip=True)
23
 
24
- if 2 <= len(text.split()) <= 5:
25
- return text
 
 
26
 
27
- # Fallback: search nearby text
28
- text = parent.get_text("\n", strip=True)
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
- # BEST: mailto-based extraction
69
- for a in page_soup.find_all("a", href=True):
70
- if "mailto:" in a["href"]:
71
- email = a["href"].replace("mailto:", "").strip()
 
72
 
73
- name = find_name_near_email(a)
 
74
 
75
- results.append({
76
- "name": name,
77
- "email": email
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 (Accurate Version)",
114
- description="Extracts correct professor names and emails"
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()