![]() |
|
[Tut] How to Extract Emails from any Website using Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Extract Emails from any Website using Python? (/thread-99945.html) |
[Tut] How to Extract Emails from any Website using Python? - xSicKxBot - 09-14-2022 How to Extract Emails from any Website using Python? 5/5 – (1 vote) ![]() The article begins by formulating a problem regarding how to extract emails from any website using Python, gives you an overview of solutions, and then goes into great detail about each solution for beginners. At the end of this article, you will know the results of comparing methods of extracting emails from a website. Continue reading to find out the answers. You may want to read out the disclaimer on web scraping here: Recommended Tutorial: Is WebScraping Legal?You can find the full code of both web scrapers on our GitHub here. ![]() Problem Formulation Marketers build email lists to generate leads. Statistics show that 33% of marketers send weekly emails, and 26% send emails multiple times per month. An email list is a fantastic tool for both company and job searching. For instance, to find out about employment openings, you can hunt up an employee’s email address of your desired company. However, manually locating, copying, and pasting emails into a CSV file takes time, costs money, and is prone to error. There are a lot of online tutorials for building email extraction bots. When attempting to extract email from a website, these bots experience some difficulty. The issues include the lengthy data extraction times and the occurrence of unexpected errors. Then, how can you obtain an email address from a company website in the most efficient manner? How can we use robust programming Python to extract data? Method Summary This post will provide two ways to extract emails from websites. They are referred to as Direct Email Extraction and Indirect Email Extraction, respectively. Our Python code will search for emails on the target page of a given company or specific website when using the direct email extraction method.For instance, when a user enters “www.scrapingbee.com” into their screen, our Python email extractor bot scrapes the website’s URLs. Then it uses a regex library to look for emails before saving them in a CSV file. The second method, the indirect email extraction method, leverages Google.com’s Search Engine Result Page (SERP) to extract email addresses instead of using a specific website.For instance, a user may type “scrapingbee.com” as the website name. The email extractor bot will search on this term and return the results to the system. The bot then stores the email addresses extracted using regex into a CSV file from these search results. In the next section, you will learn more about these methods in more detail.These two techniques are excellent email list-building tools. The main issue with alternative email extraction techniques posted online, as was already said, is that they extract hundreds of irrelevant website URLs that don’t contain emails. The programming running through these approaches takes several hours. Discover our two excellent methods by continuing reading. [b]Solution[/b] [b]Method 1 Direct Email Extraction[/b] This method will outline the step-by-step process for obtaining an email address from a particular website. [b]Step 1: Install Libraries.[/b] Using the Code: pip
Code: pip install re
pip install request
pip install bs4
pip install python-collections
pip install urlib
pip install pandas
pip install tldImport the libraries as shown below: Code: import re
import requests
from bs4 import BeautifulSoup
from collections import deque
from urllib.parse import urlsplit
import pandas as pd
from tld import get_fldAsk the user to enter the desired website for extracting emails with the Code: input()Code: user_urlCode: user_url = input("Enter the website url to extract emails: ")
if "https://" in user_url: user_url = user_url
else: user_url = "https://"+ user_urlBefore we start writing the code, let’s define some variables. Create two variables using the command below to store the URLs of scraped and un-scraped websites: Code: unscraped_url = deque([user_url])
scraped_url = set()You can save the URLs of websites that are not scraped using the Code: dequeAs seen below, the variable Code: list_emailsCode: list_emails = set()Utilizing a set data type is primarily intended to eliminate duplicate emails and keep just unique emails. Let us proceed to the next step of our main program to extract email from a website. [b]Step 5[b]: Adding Urls for Content Extraction.[/b][/b] Web page URLs are transferred from the variable Code: unscraped_urlCode: scrapped_urlCode: while len(unscraped_url): url = unscraped_url.popleft() scraped_url.add(url)The Code: popleft()Code: dequeCode: urlThen the Code: urlCode: scraped_urlCode: add()[b]Step 6[b]: Splitting of URLs and merging them with base URL.[/b][/b] The website contains relative links that you cannot access directly. Therefore, we must merge the relative links with the base URL. We need the Code: urlsplit()Code: parts = urlsplit(url)Create a Code: partsCode: SplitResult(scheme='https', netloc='www.scrapingbee.com', path='/', query='', fragment='')As an example shown above, the URL https://www.scrapingbee.com/ is divided into Code: schemeCode: netlocCode: pathThe split result’s Code: netlocCode: base_url = "{0.scheme}://{0.netloc}".format(parts)Next, we create the basic URL by merging the Code: schemeCode: netlocBase URL means the main website’s URL is what you type into the browser’s address bar when you input it. If the user enters relative URLs when requested by the program, we must then convert them back to base URLs. We can accomplish this by using the command: Code: if '/' in parts.path: part = url.rfind("/") path = url[0:part + 1]
else: path = urlLet us understand how each line of the above command works. Suppose the user enters the following URL: This URL is a relative link, and the above set of commands will convert it to a base URL (https://www.scrapingbee.com). Let’s see how it works. If the condition finds that there is a “ Code: /Code: /Code: rfind()Code: /Next line of code stores the URL from 0 to 27 + 1, i.e., 28th item position, i.e., https://www.scrapingbee.com/. Thus, it converts to the base URL. In the last command, If there is no relative link from the URL, it is the same as the base URL. That links are in the Code: pathThe following command prints the URLs for which the program is scraping. Code: print("Searching for Emails in %s" % url)The HTML Get Request Command access the user-entered website. Code: response = requests.get(url)Then, extract all email addresses from the response variable using a regular expression, and update them to the Code: list_emailsCode: new_emails = ((re.findall(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", response.text, re.I)))
list_emails.update(new_emails)The regression is built to match the email address syntax displayed in the new emails variable. The regression format pulls the email address from the website URL’s content with the Code: response.textCode: re.ICode: list_emailsThe next is to find all of the website’s URL links and extract them in order to retrieve the email addresses that are currently available. You can utilize a powerful, beautiful soup module to carry out this procedure. Code: soup = BeautifulSoup(response.text, 'lxml')A beautiful soup function parses the HTML document of the webpage the user has entered, as shown in the above command. You can find out how many emails have been extracted with the following command. Code: print("Email Extracted: " + str(len(list_emails)))The URLs related to the website can be found with “ Code: a hrefCode: for tag in soup.find_all("a"): if "href" in tag.attrs: weblink = tag.attrs["href"] else: weblink = ""Beautiful soups find all the anchor tag “ Code: aThen if Code: hrefCode: if weblink.startswith('/'): weblink = base_url + weblink
elif not weblink.startswith('https'): weblink = path + weblinkThe Code: hrefCode: /For instance, you can see the following URL on the scraping bee website:
Thus, the above command combines the extracted Code: hrefFor example, in the case of pricing, the weblink variable is as follows: Code: Weblink = "https://www.scrapingbee.com/#pricing"In some cases, Code: hrefCode: /Code: httpsFor example, Code: hrefCode: <a href="mailto:support@scrapingbee.com?subject=Enterprise plan&body=Hi there, I'd like to discuss the Enterprise plan." class="btn btn-sm btn-black-o w-full mt-13">1-click quote</a>Now let’s complete the code with the following command: Code: if not weblink in unscraped_url and not weblink in scraped_url: unscraped_url.append(weblink) print(list_emails)The above command appends URLs not scraped to the unscraped Code: urlRun the program. What if the program doesn’t work? Are you getting errors or exceptions of Missing Schema, Connection Error, or Invalid URL? Some of the websites you aren’t able to access for some reason. Don’t worry! Let’s see how to hit these errors. Use the Try Exception command to bypass the errors as shown below: Code: try: response = requests.get(url)
except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError, requests.exceptions.InvalidURL): continueInsert the command before the email regex command. Precisely, place this command above the Code: new_emailsRun the program now. Did the program work? Does it keep on running for several hours and not complete it? The program searches and extracts all the URLs from the given website. Also, It is extracting links from other domain name websites. For example, the Scraping Bee website has URLs such as https://seekwell.io/., https://codesubmit.io/, and more. A well-built website has up to 100 links for a single page of a website. So the program will take several hours to extract the links. Sorry about it. You have to face this issue to get your target emails. Bye Bye, the article ends here…….. No, I am just joking! Fret Not! I will give you the best solution in the next step. [b]Step 8[b]: Fix the code problems.[/b][/b] Here is the solution code for you: Code: if base_url in weblink: # code1 if ("contact" in weblink or "Contact" in weblink or "About" in weblink or "about" in weblink or 'CONTACT' in weblink or 'ABOUT' in weblink or 'contact-us' in weblink): #code2 if not weblink in unscraped_url and not weblink in scraped_url: unscraped_url.append(weblink)First off, apply code 1, which specifies that you only include base URL websites from links weblinks to prevent scraping other domain name websites from a specific website. Since the majority of emails are provided on the contact us and about web pages, only those links from those sites will be extracted (Refer to code 2). Other pages are not considered. Finally, unscraped URLs are added to the Code: unscrapped_url[b]Step 9[b]: Exporting the Email Address to CSV file.[/b][/b] Finally, we can save the email address in a CSV file ( Code: email2.csvCode: url_name = "{0.netloc}".format(parts)
col = "List of Emails " + url_name
df = pd.DataFrame(list_emails, columns=[col])
s = get_fld(base_url)
df = df[df[col].str.contains(s) == True]
df.to_csv('email2.csv', index=False)We use Code: get_fldCode: sWe include only emails ending with the website’s first-level domain name in the data frame. Other domain names that do not belong to the base URL are ignored. Finally, the data frame transfers emails to a CSV file. As previously stated, a web admin can maintain up to 100 links per page. Because there are more than 30 hyperlinks on each page for a normal website, it will still take some time to finish the program. If you believe that the software has extracted enough email, you may manually halt it using Code: try except KeyboardInterruptCode: raise SystemExitCode: try:
while len(unscraped_url):
… if base_url in weblink: if ("contact" in weblink or "Contact" in weblink or "About" in weblink or "about" in weblink or 'CONTACT' in weblink or 'ABOUT' in weblink or 'contact-us' in weblink): if not weblink in unscraped_url and not weblink in scraped_url: unscraped_url.append(weblink) url_name = "{0.netloc}".format(parts) col = "List of Emails " + url_name df = pd.DataFrame(list_emails, columns=[col]) s = get_fld(base_url) df = df[df[col].str.contains(s) == True] df.to_csv('email2.csv', index=False) except KeyboardInterrupt: url_name = "{0.netloc}".format(parts) col = "List of Emails " + url_name df = pd.DataFrame(list_emails, columns=[col]) s = get_fld(base_url) df = df[df[col].str.contains(s) == True] df.to_csv('email2.csv', index=False) print("Program terminated manually!") raise SystemExitRun the program and enjoy it… Let’s see what our fantastic email scraper application produced. The website I have entered is www.abbott.com. Output: ![]() [b]Method 2 Indirect Email Extraction[/b] You will learn the steps to extract email addresses from Google.com using the second method. [b]Step 1: Install Libraries.[/b] Using the Code: pip
Code: pip install bs4
pip install pandas
pip install re
pip install request
pip install timeImport the libraries. Code: from bs4 import BeautifulSoup
import pandas as pd
import re
import requests
from tld import get_fld
import time[b]Step 3: Constructing Search Query.[/b] The search query is written in the format “ Code: @websitename.comCreate an input for the user to enter the URL of the website. Code: user_keyword = input("Enter the Website Name: ")
user_keyword = str('"@') + user_keyword +' " 'The format of the search query is “ Code: @websitename.comCode: user_keyword[b]Step 4: Define Variables.[/b] Before moving on to the heart of the program, let’s first set up the variables. Code: page = 0
list_email = set()You can move through multiple Google search results pages using the Code: pageCode: list_email[b]Step 5[b]: Requesting Google Page.[/b][/b] In this step, you will learn how to create a Google URL link using a user keyword term and request the same. The Main part of coding starts as below: Code: while page <= 100: print("Searching Emails in page No " + str(page)) time.sleep(20.00) google = "https://www.google.com/search?q=" + user_keyword + "&ei=dUoTY-i9L_2Cxc8P5aSU8AI&start=" + str(page) response = requests.get(google) print(response)Let’s examine what each line of code does.
Before creating a google variable, let us learn more about the google search URL. Suppose you search the keyword “Germany” on google.com. Then the Google search URL will be as follows
If you click the second page of the Google search result, then the link will be as follows:
How does that link work?
[b]Step 6[b]: Extracting Email Address.[/b][/b] In this step, you will learn how to extract the email address from the google search result contents. Code: soup = BeautifulSoup(response.text, 'html.parser')
new_emails = ((re.findall(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", soup.text, re.I)))
list_email.update(new_emails)
page = page + 10The Beautiful soup parses the web page and extracts the content of html web page. With the regex Code: findall()Code: list_emailCode: n = len(user_keyword)-1
base_url = "https://www." + user_keyword[2:n]
col = "List of Emails " + user_keyword[2:n]
df = pd.DataFrame(list_email, columns=[col])
s = get_fld(base_url)
df = df[df[col].str.contains(s) == True]
df.to_csv('email3.csv', index=False)And finally, target emails are saved to the CSV file from the above lines of code. The list item in the Code: user_keywordRun the program and see the output. ![]() [b]Method 1 Vs. Method 2 [/b] Can we determine which approach is more effective for building an email list: Method 1 Direct Email Extraction or Method 2 Indirect Email Extraction? The output’s email list was generated from the website abbot.com. Let’s contrast two email lists that were extracted using Methods 1 and 2.
Thus, we are unable to recommend one procedure over another. Both techniques provide fresh email lists. As a result, both of these methods will increase your email list. [b]Summary[/b] Building an email list is crucial for businesses and freelancers alike to increase sales and leads. This article offers instructions on using Python to retrieve email addresses from websites. The best two methods to obtain email addresses are provided in the article. In order to provide a recommendation, the two techniques are finally compared. The first approach is a direct email extractor from any website, and the second method is to extract email addresses using Google.com. Regex Humor [i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
|