jayanth-2005 commited on
Commit
2ca91e4
Β·
verified Β·
1 Parent(s): 87f23b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -44
app.py CHANGED
@@ -6,54 +6,60 @@ import gradio as gr
6
 
7
 
8
  # ---------------------------------
9
- # Extract name from same block
10
  # ---------------------------------
11
- def extract_name(tag):
12
- # First: look for headings (BEST source)
13
- for h in tag.find_all(["h1", "h2", "h3", "h4", "strong", "b"]):
14
- text = h.get_text(strip=True)
15
-
16
- if is_valid_name(text):
17
- return text
18
-
19
- # Second: scan all lines
20
- text = tag.get_text("\n", strip=True)
21
- lines = text.split("\n")
22
-
23
- for line in lines:
24
- line = line.strip()
25
 
26
- if is_valid_name(line):
27
- return line
28
 
29
- return "Not found"
30
 
31
 
32
  # ---------------------------------
33
- # Strong validation filter
34
  # ---------------------------------
35
  def is_valid_name(text):
36
  text_lower = text.lower()
37
 
38
- # ❌ Reject junk
39
  if any(word in text_lower for word in [
40
  "iit", "university", "college",
41
- "school", "department",
42
- "email", "phone", "office"
43
  ]):
44
  return False
45
 
46
- # ❌ Reject emails
47
  if "@" in text:
48
  return False
49
 
50
  words = text.split()
51
 
52
- # βœ… Accept realistic names
53
- if 2 <= len(words) <= 5:
54
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- return False
57
 
58
  # ---------------------------------
59
  # Main scraper
@@ -64,7 +70,6 @@ def get_professor_details(college_url):
64
  response = requests.get(college_url, headers=headers, timeout=10)
65
  soup = BeautifulSoup(response.text, "html.parser")
66
 
67
- # Find faculty pages
68
  keywords = ["faculty", "people", "staff", "directory"]
69
  faculty_links = []
70
 
@@ -76,22 +81,31 @@ def get_professor_details(college_url):
76
  pages_to_scan = faculty_links if faculty_links else [college_url]
77
 
78
  results = []
 
79
 
80
  for page in pages_to_scan:
81
  try:
82
  res = requests.get(page, headers=headers, timeout=10)
83
  page_soup = BeautifulSoup(res.text, "html.parser")
84
 
85
- # πŸ”₯ KEY: scan structured blocks
86
- for tag in page_soup.find_all(["div", "tr", "li"]):
87
  text = tag.get_text(" ", strip=True)
88
 
89
  emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)
90
 
91
  if emails:
92
- name = extract_name(tag)
93
-
94
  for email in emails:
 
 
 
 
 
 
 
 
 
 
 
95
  results.append({
96
  "name": name,
97
  "email": email
@@ -103,18 +117,9 @@ def get_professor_details(college_url):
103
  if not results:
104
  return "No data found."
105
 
106
- # Remove duplicates
107
- seen = set()
108
- cleaned = []
109
-
110
- for r in results:
111
- if r["email"] not in seen:
112
- seen.add(r["email"])
113
- cleaned.append(r)
114
-
115
  # Format output
116
  output = ""
117
- for r in cleaned:
118
  output += f"Name: {r['name']}\nEmail: {r['email']}\n\n"
119
 
120
  return output
@@ -130,8 +135,8 @@ iface = gr.Interface(
130
  fn=get_professor_details,
131
  inputs=gr.Textbox(label="Enter College Website URL"),
132
  outputs=gr.Textbox(label="Professor Details"),
133
- title="πŸŽ“ Professor Scraper (Final Working)",
134
- description="Extracts professor names and emails from college websites"
135
  )
136
 
137
  iface.launch()
 
6
 
7
 
8
  # ---------------------------------
9
+ # Email β†’ Name fallback
10
  # ---------------------------------
11
+ def name_from_email(email):
12
+ username = email.split("@")[0]
13
+ parts = re.split(r"[._]", username)
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ if len(parts) == 1:
16
+ parts = re.findall('[A-Za-z][^A-Z]*', username)
17
 
18
+ return " ".join([p.capitalize() for p in parts if p])
19
 
20
 
21
  # ---------------------------------
22
+ # Validate real name
23
  # ---------------------------------
24
  def is_valid_name(text):
25
  text_lower = text.lower()
26
 
 
27
  if any(word in text_lower for word in [
28
  "iit", "university", "college",
29
+ "department", "school",
30
+ "faculty", "staff"
31
  ]):
32
  return False
33
 
 
34
  if "@" in text:
35
  return False
36
 
37
  words = text.split()
38
 
39
+ return 2 <= len(words) <= 5
40
+
41
+
42
+ # ---------------------------------
43
+ # Find name near email
44
+ # ---------------------------------
45
+ def find_name_near(tag):
46
+ # 1. Check headings (BEST)
47
+ for h in tag.find_all(["h1", "h2", "h3", "h4", "strong", "b"]):
48
+ text = h.get_text(strip=True)
49
+ if is_valid_name(text):
50
+ return text
51
+
52
+ # 2. Check nearby lines
53
+ text = tag.get_text("\n", strip=True)
54
+ lines = text.split("\n")
55
+
56
+ for line in lines:
57
+ line = line.strip()
58
+ if is_valid_name(line):
59
+ return line
60
+
61
+ return None
62
 
 
63
 
64
  # ---------------------------------
65
  # Main scraper
 
70
  response = requests.get(college_url, headers=headers, timeout=10)
71
  soup = BeautifulSoup(response.text, "html.parser")
72
 
 
73
  keywords = ["faculty", "people", "staff", "directory"]
74
  faculty_links = []
75
 
 
81
  pages_to_scan = faculty_links if faculty_links else [college_url]
82
 
83
  results = []
84
+ seen = set()
85
 
86
  for page in pages_to_scan:
87
  try:
88
  res = requests.get(page, headers=headers, timeout=10)
89
  page_soup = BeautifulSoup(res.text, "html.parser")
90
 
91
+ for tag in page_soup.find_all(["div", "li", "td"]):
 
92
  text = tag.get_text(" ", strip=True)
93
 
94
  emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)
95
 
96
  if emails:
 
 
97
  for email in emails:
98
+ if email in seen:
99
+ continue
100
+ seen.add(email)
101
+
102
+ # πŸ”₯ Try extracting real name
103
+ name = find_name_near(tag)
104
+
105
+ # πŸ”₯ Fallback to email-based name
106
+ if not name:
107
+ name = name_from_email(email)
108
+
109
  results.append({
110
  "name": name,
111
  "email": email
 
117
  if not results:
118
  return "No data found."
119
 
 
 
 
 
 
 
 
 
 
120
  # Format output
121
  output = ""
122
+ for r in results:
123
  output += f"Name: {r['name']}\nEmail: {r['email']}\n\n"
124
 
125
  return output
 
135
  fn=get_professor_details,
136
  inputs=gr.Textbox(label="Enter College Website URL"),
137
  outputs=gr.Textbox(label="Professor Details"),
138
+ title="πŸŽ“ Professor Scraper (Hybrid AI Logic)",
139
+ description="Finds names near emails and falls back to smart guessing"
140
  )
141
 
142
  iface.launch()