How to Perform Web Scraping with Python and RegEx

I will be using Python version 3.5.2 to scrape email addresses from a particular website. The code is pretty simple! The RegEx defines a search pattern which in this tutorial is used to find email addresses.

For your own purposes, you may need a different search pattern depending on what you are looking to scrape. Here is a Python RegEx cheat sheet: https://www.debuggex.com/cheatsheet/regex/python

Here is the code for web_scraper.py:

import urllib.request, re

#Define the target website
target = 'https://irishjudoassociation.ie/clubs/'

#Get the data from the website
text = urllib.request.urlopen(target).read().decode('utf-8')

#Define the regular expression
regex = re.compile(r'[\w.-]+@[\w.-]+')

#Compile using the regular expression
email = re.findall(regex, text)

#Display the email addresses in an list
print(email)

#Separate the list elements by row in a csv file (out.csv)
import csv
with open("out.csv","w") as f:
  wr = csv.writer(f,delimiter="\n")
  wr.writerow(email)

Run the script by typing ‘python3 web_scraper.py’ in the command line.

Leave a Reply

Your email address will not be published. Required fields are marked *