Open In App

Email Id Extractor Project from sites in Scrapy Python

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

Scrapy is open-source web-crawling framework written in Python used for web scraping, it can also be used to extract data for general-purpose. First all sub pages links are taken from the main page and then email id are scraped from these sub pages using regular expression. 

This article shows the email id extraction from geeksforgeeks site as a reference.

Email ids to be scraped from geeksforgeeks site - ['feedback@geeksforgeeks.org', 'classes@geeksforgeeks.org', 'complaints@geeksforgeeks.org','review-team@geeksforgeeks.org']

How to create Email ID Extractor Project using Scrapy?

1. Installation of packages - run following command from terminal 

pip install scrapy 
pip install scrapy-selenium

2. Create project - 

scrapy startproject projectname (Here projectname is geeksemailtrack) 
cd projectname 
scrapy genspider spidername (Here spidername is emails)

3) Add code in settings.py file to use scrapy-selenium

from shutil import which 
SELENIUM_DRIVER_NAME = 'chrome' 
SELENIUM_DRIVER_EXECUTABLE_PATH = which('chromedriver') 
SELENIUM_DRIVER_ARGUMENTS=[]
DOWNLOADER_MIDDLEWARES = { 
'scrapy_selenium.SeleniumMiddleware': 800 
}

4) Now download chrome driver for your chrome and put it near to your chrome scrapy.cfg file. To download chrome driver refer this site - To download chrome driver

Directory structure - 


Step by Step Code - 

1. Import all required libraries - 

# web scraping framework
import scrapy

# for regular expression
import re

# for selenium request
from scrapy_selenium import SeleniumRequest

# for link extraction
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor

 
 

2. Create start_requests function to hit the site from selenium. You can add your own URL. 
 


 

def start_requests(self):
    yield SeleniumRequest(
        url="https://www.geeksforgeeks.org/",
        wait_time=3,
        screenshot=True,
        callback=self.parse,
        dont_filter=True
    )

 
 


3. Create parse function: 
 


 

def parse(self, response):
        # this helps to get all links from source code
        links = LxmlLinkExtractor(allow=()).extract_links(response)

        # Finallinks contains links url
        Finallinks = [str(link.url) for link in links]

        # links list for url that may have email ids
        links = []

        # filtering and storing only needed url in links list
        # pages that are about us and contact us are the ones that have email ids
        for link in Finallinks:
            if ('Contact' in link or 'contact' in link or 'About' in link or 'about' in link or 'CONTACT' in link or 'ABOUT' in link):
                links.append(link)

        # current page url also added because few sites have email ids on there main page
        links.append(str(response.url))



        # parse_link function is called for extracting email ids
        l = links[0]
        links.pop(0)

        # meta helps to transfer links list from parse to parse_link
        yield SeleniumRequest(
            url=l,
            wait_time=3,
            screenshot=True,
            callback=self.parse_link,
            dont_filter=True,
            meta={'links': links}
        )

 
 

Explanation of parse function - 


 

  • In the following lines  all links are extracted from https://www.geeksforgeeks.org/ response.
links = LxmlLinkExtractor(allow=()).extract_links(response) 
Finallinks = [str(link.url) for link in links] 
  • Finallinks is list containing all links.
  • To avoid unnecessary links we put filter that, if links belong to contact and about page then only we scrape details from that page.
for link in Finallinks: 
if ('Contact' in link or 'contact' in link or 'About' in link or 'about' in link or 
or 'CONTACT' in link or 'ABOUT' in 
link): 
links.append(link) 
  • This Above filter is not necessary but sites do have lots of tags(links) and due to this, if site has 50 subpages in site then it will extract email from these 50 sub URLs. it is assumed that emails are mostly on home page, contact page, and about page so this filter help to reduce time wastage of scraping those URL that might not have email ids.
  • The links of pages that may have email ids are requested one by one and email ids are scraped using regular expression.


 

4. Create parse_link function code: 


 

Explanation of parse_link function: 
By response.text we get the all source code of the requested URL. The regex expression ‘\w+@\w+\.{1}\w+’ used here could be translated to something like this Look for every piece of string that starts with one or more letters, followed by an at sign (‘@’), followed by one or more letters with a dot in the end. 
After that it should have one or more letters again. Its a regex used for getting email id.

5. Create parsed function -
 

Explanation of Parsed function: 
The above regex expression also leads to garbage values like select@1.13 in this scraping email id from geeksforgeeks, we know select@1.13 is not a email id. The parsed function filter applies filter that only takes emails containing '.com' and ".in".
 

Run the spider using following command - 

scrapy crawl spidername (spidername is name of spider)

Garbage value in scraped emails: 


Final scraped emails: 
 


 

Working video of above code -

Reference - linkextractors 
 


Practice Tags :

Similar Reads