Scrape Business Emails from Instagram using Python

EmilDev
4 min readApr 7, 2023

--

Do you want to expand your email marketing list? Instagram can be an excellent source of potential customers and clients. Fortunately, with a Python bot, you can easily scrape business emails from Instagram. Today, I will guide you through the steps to create a Python bot that scrapes business emails from Instagram.

Step 1: Install Required Libraries

Before we begin, you need to install the necessary libraries to scrape data from the web. You can install them using the following pip command (just copy and paste this into your terminal):

pip install requests beautifulsoup4

Step 2: Set up the Bot

Next, you need to set up your bot. You can use the following code as a starting point, make sure not to disclose your login details with anyone, I recommend doing this project in an environment like PyCharm.

import requests
from bs4 import BeautifulSoup

URL = "https://www.instagram.com/"
USERNAME = "your_instagram_username"
PASSWORD = "your_instagram_password"
SEARCH_QUERY = "your_search_query"

In the above code, replace your_instagram_username with your Instagram username, your_instagram_password with your Instagram password, and your_search_query with the search query for the type of businesses you want to target e.g. ‘interior design’ would find all interior design businesses listed on instagram with an email.

Step 3: Log in to Instagram

To access Instagram, you need to log in to your account using Python code. You can use the following code to do that.

session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.instagram.com/",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded"
})

req = session.get(URL)
soup = BeautifulSoup(req.content, "html.parser")
login_data = {
"username": USERNAME,
"password": PASSWORD
}
session.post("https://www.instagram.com/accounts/login/ajax/", data=login_data, headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://www.instagram.com/",
"x-csrftoken": soup.find('input', {'name': 'csrf_token'})['value']
})

You will most likely need to download the Firefox browser for this as it recommended for the code to work, you can download it on their website here if you do not already have it installed.

Step 4: Scrape Business Emails

Nice! You can now scrape the business emails using the following code.

email_list = []
search_url = f"https://www.instagram.com/web/search/topsearch/?context=blended&query={SEARCH_QUERY}&rank_token=0.3953592318270893&include_reel=true"
response = session.get(search_url)
data = response.json()
for user in data['users']:
user_url = f"https://www.instagram.com/{user['user']['username']}/"
user_req = session.get(user_url)
user_soup = BeautifulSoup(user_req.content, "html.parser")
user_email = user_soup.select_one("a[href^='mailto:']")
if user_email:
email_list.append(user_email.text)

In the above code, search_url is the URL for the search results page on Instagram. The search query is included in the URL. The bot then scrapes all users. The result is in JSON format so we will iterate over it using ‘for user in data[‘users’] to get the account names and emails.

Full Code

import requests
from bs4 import BeautifulSoup

# Define the search query
SEARCH_QUERY = "business"

# Create a session object
session = requests.Session()

# Set the User-Agent header to mimic a web browser
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

session.headers.update(headers)

# Create an empty list to store the emails
email_list = []

# Define the search URL
search_url = f"https://www.instagram.com/web/search/topsearch/?context=blended&query={SEARCH_QUERY}&rank_token=0.3953592318270893&include_reel=true"

# Send a GET request to the search URL and extract the JSON data
response = session.get(search_url)
data = response.json()

# Loop through the data and extract the usernames
for user in data['users']:
# Create the URL for the user's profile
user_url = f"https://www.instagram.com/{user['user']['username']}/"

# Send a GET request to the user's profile and extract the HTML content
user_req = session.get(user_url)
user_soup = BeautifulSoup(user_req.content, "html.parser")

# Find the email link on the user's profile page
user_email = user_soup.select_one("a[href^='mailto:']")
if user_email:
email_list.append(user_email.text)

# Print the list of emails
print(email_list)

In conclusion, scraping business emails from Instagram can be a useful way to find leads for your business, grow your email listing or network with potential collaborators. In this article, we’ve shown you how to create a Python bot that can scrape business emails from Instagram profiles. We’ve explained the step-by-step process involved in the code and provided some tips on how to optimise your scraping process.

If you have found this useful please consider following and sending 1–50 claps, thank you!

It’s important to note that web scraping can have ethical and legal implications, and it’s essential to respect the website’s terms of service and any applicable laws and regulations. Additionally, some websites may have measures in place to prevent scraping, so it’s important to exercise caution and be respectful in your scraping efforts.

Happy Coding!

--

--

EmilDev

Python / Crypto / Web Development / Machine Learning