Spaces:
Sleeping
Sleeping
Update app.py
Browse files5 unmodified lines678910111213141516171819202122232425262728293031323334353637383925 unmodified lines656667686970717273747576777879808128 unmodified lines1101111121131141151161175 unmodified lines# ---------------------------------# Extract closest name near email# ---------------------------------def find_name_near_email(tag):# Look upward in same blockparent = tag.find_parent()if not parent:return "Not found"# Check inside same blockcandidates = parent.find_all(["h1", "h2", "h3", "h4", "strong", "b"])for c in candidates:text = c.get_text(strip=True)if 2 <= len(text.split()) <= 5:return text# Fallback: search nearby texttext = parent.get_text("\n", strip=True)lines = text.split("\n")for line in lines:if 2 <= len(line.split()) <= 5 and "@" not in line:if not any(word in line.lower() for word in ["department", "school", "university", "iit"]):return linereturn "Not found"25 unmodified linesres = requests.get(page, headers=headers, timeout=10)page_soup = BeautifulSoup(res.text, "html.parser")# BEST: mailto-based extractionfor a in page_soup.find_all("a", href=True):if "mailto:" in a["href"]:email = a["href"].replace("mailto:", "").strip()name = find_name_near_email(a)results.append({"name": name,"email": email})except:continue28 unmodified linesfn=get_professor_details,inputs=gr.Textbox(label="Enter College Website URL"),outputs=gr.Textbox(label="Professor Details"),title="π Professor Scraper (Accurate Version)",description="Extracts correct professor names and emails")iface.launch()5 unmodified lines67891011121314151617181920212223242526272829303132333435363725 unmodified lines636465666768697071727374757677787980818228 unmodified lines1111121131141151161171185 unmodified lines# ---------------------------------# Extract name from same block# ---------------------------------def extract_name(tag):# Step 1: check headings inside same blockfor h in tag.find_all(["h1", "h2", "h3", "h4", "strong", "b"]):name = h.get_text(strip=True)if 2 <= len(name.split()) <= 5:return name# Step 2: fallback to nearby texttext = tag.get_text("\n", strip=True)lines = text.split("\n")for line in lines:line = line.strip()if "@" in line:continueif any(word in line.lower() for word in ["department", "school", "iit", "university"]):continueif 2 <= len(line.split()) <= 5:return linereturn "Not found"25 unmodified linesres = requests.get(page, headers=headers, timeout=10)page_soup = BeautifulSoup(res.text, "html.parser")# π₯ KEY: scan structured blocksfor tag in page_soup.find_all(["div", "li", "td"]):text = tag.get_text(" ", strip=True)emails = re.findall(r"[\w\.-]+@[\w\.-]+", text)if emails:name = extract_name(tag)for email in emails:results.append({"name": name,"email": email})except:continue28 unmodified linesfn=get_professor_details,inputs=gr.Textbox(label="Enter College Website URL"),outputs=gr.Textbox(label="Professor Details"),title="π Professor Scraper (Final Working)",description="Extracts professor names and emails from college websites")iface.launch()
app.py+35-34