forked from james-see/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbs4_email_regex-example.py
More file actions
40 lines (34 loc) · 979 Bytes
/
bs4_email_regex-example.py
File metadata and controls
40 lines (34 loc) · 979 Bytes
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
"""
What: bs4 get email from beautiful soup object
Author: James Campbell
Date: 2015-10-09
Updated Date: 3 July 2019
"""
from bs4 import BeautifulSoup
import re
import sys
def get_emails(soupcontent):
"""
soupcontent: expected to be a bs4 object
Description: This functions gets emails from bs4 object and returns a list
"""
emaillist = []
soupere = soupcontent.find_all(
text=re.compile(
r"\S[a-zA-Z0-9._%+-].*?[a-zA-Z0-9_%+-]+@[a-zA-Z0-9.-]+?\.[a-zA-Z]{2,4}?\b"
)
)
for soupe in soupere:
soupe = soupe.strip()
if soupe == "":
continue
emaillist.append(soupe)
return emaillist
htmlcontent = """<html><head></head><body>
<p>This is an email: [email protected] and this is not: james.</p>"""
soupobject = BeautifulSoup(htmlcontent, "html.parser")
finallist = get_emails(soupobject)
# prove that it worked
for email in finallist:
print(email)
sys.exit()