-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoneEmailScraper.py
More file actions
61 lines (52 loc) · 1.8 KB
/
Copy pathphoneEmailScraper.py
File metadata and controls
61 lines (52 loc) · 1.8 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! python3
# pulls phone numbers and email addresses from text copied to the clipboard
import pyperclip
import re
'''
phoneRegex:
# country code accounting for whitespace
# spacer
# 3 digit area code incl. optional bracket notation
# spacer
# 7 digit phone number in 2 seperate capture groups
# Matches most common extension notation a flexible distance
away from the initial phone number
'''
phoneRegex = re.compile(r'''
(?i)
(?<!\w)(?P<c>\d)?
\D?
\(?(?P<a>\d{3})?\)?
\D?
(?P<n1>\d{3})\D?(?P<n2>\d{4})
((\D*(x|extension)\D{,3}(?P<ext>\d+)))?
''', re.X)
# matches any non-whitespace character on either side of the @
emailRegex = re.compile(r'''(\S+\@\S+)''', re.X)
copiedText = str(pyperclip.paste())
phoneMatch = []
emailMatch = []
'''this if-mess translates each various phone number match into it's own
string (accounting for differing/missing notation) and appends
it to a list of phone numbers'''
for number in phoneRegex.finditer(copiedText):
phoneNum = str(number.group('n1') + '.' + number.group('n2'))
if number.group('a'):
phoneNum = str(number.group('a') + '.' + phoneNum)
if number.group('c'):
phoneNum = str(number.group('c') + '.' + phoneNum)
if number.group('ext'):
phoneNum += str(' ext.' + number.group('ext'))
phoneMatch.append(phoneNum)
# finds and appends emails into a list
for email in emailRegex.findall(copiedText):
emailMatch.append(email)
'''begins each section with a report on how many of each match was
found in the text and then pipes a comma delimited string "list" to allMatch'''
allMatch = '\nphone numbers found: ' + str(len(phoneMatch)) + '\n'
for n in phoneMatch:
allMatch += n + ', '
allMatch += ('\nemails found: ' + str(len(emailMatch)) + '\n')
for n in emailMatch:
allMatch += n + ', '
pyperclip.copy(allMatch)