Introduction
Webscraping is one of the most used techniques to learn about data on the Internet. It is the art of extracting the valuable and required data from a webpage which are considered as the input value for performing various computational operations to generate useful information. In this article, we will learn how we can collect email data published on any webpage. We are using Python, one of the most popular programming languages, to extract data elements value, as it has rich libraries that help to perform various required activities.
The following steps will help you to learn how to find an email on any webpage.
Step 1
We need to import all the essential libraries for our program.
- BeautifulSoup: It is a Python library for extracting data out of HTML and XML files.
- requests: The requests library allows us to send HTTP requests using Python.
- urllib.parse: This module provides functions for manipulating URLs and their component parts, to either break them down or build them up.
- collections: It provides different types of containers
- re: A module that handles regular expressions.
- #import packages
- from bs4 import BeautifulSoup
- import requests
- import requests.exceptions
- from urllib.parse import urlsplit
- from collections import deque
- import re
Step 2
Select the URL for extracting an email from a given URL.
- # a queue of urls to be crawled
- new_urls = deque(['https://www.gtu.ac.in/page.aspx?p=ContactUsA'])
Step 3
We have to process the given URL only once, so keep track of your processed URLs.
- # a set of urls that we have already crawled
- processed_urls = set()
Step 4
While crawling the given URL, we may encounter more than one the email-ID so keep them in the collections.
- # a set of crawled emails
- emails = set()
Step 5
Time to start crawling, we need to crawl all the URLs in the queue, maintain the list of crawled URLs & get the page content from the webpage. If any error is encountered, move to the next page.
- # process urls one by one until we exhaust the queue
- while len(new_urls):
- # move next url from the queue to the set of processed urls
- url = new_urls.popleft()
- processed_urls.add(url)
- # get url's content
- print("Processing %s" % url)
- try:
- response = requests.get(url)
- except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
- # ignore pages with errors
- continue

Abhidith ShettyPosted Sep 20, 2021, 7:35 AM
Hi sir, how do I scrape email domains from multiple urls?