-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_scrape.py
32 lines (22 loc) · 1.06 KB
/
contact_scrape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
with open(file=file_path) as doc:
soup = BeautifulSoup(doc, 'html.parser')
table_cells = soup.find_all('td')
names = [i.find('a').contents[0].title() for i in table_cells if i.find('a') != None]
emails = [i.find('a').attrs['href'].replace('mailto:','').lower() for i in table_cells if i.find('a') != None]
departments = [i.next_sibling.next_sibling.contents[0] for i in table_cells if i.find('a') != None]
phones = [i.next_sibling.next_sibling.next_sibling.next_sibling.contents[0] for i in table_cells if i.find('a') != None]
contacts = [(names[i], emails[i], departments[i], phones[i]) for i in range(0, len(names))]
unique_contacts = []
for contact in contacts:
if contact not in unique_contacts:
unique_contacts.append(contact)
unique_contacts.sort()
with open('output.txt', 'w') as f:
for contact in unique_contacts:
f.write('\t'.join(contact) + '\n')