Python code to scrape email address using regular expression
Programmers when learn writing web spiders or crawlers, try to write a script to parse/collect email address from website. Here I post a class in Python that can harvest email address. It uses regular expression. import re class EmailScraper(): def __init__(self): self.emails = [] def reset(self): self.emails = [] def collectAllEmail(self, htmlSource): "collects all possible email addresses from a string, but still it can miss some addresses" #example: t.s@d.com email_pattern = re.compile("[-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+") self.emails = re.findall(email_pattern, htmlSource) def collectEmail(self, htmlSource): "collects all emails that starts with mailto: in the html source string" #example: <a href="mailto:t.s@d.com"> email_pattern = re.compile("<a\s+href=\"mailto:([a-zA-Z0-9._@]*)\">", re.IGNORECASE...