Web Scraping in Python
Web Scraping in Python
mmmmmmmmmmmmmmmmmkmmmmmmmmmmmmmmmmmmmmmkmWEB SCRAPE ON
INSPIRATIONAL QUOTES USING PYTHON:
In this article, I will show you how to scrape inspirational quotes from a website using the Python
programming language. I think everyone likes to hear some inspirational quotes from time to time
and hopefully the quotes that we will scrape within this article will brighten your day.
For anyone reading this article and don’t know what web scraping is, I will define it now. Web
scraping, or simply scraping is the act or process of extracting data from a website. This means you
will learn how to extract information from a website using Python.
By using the inspection tool, I can see that the quotes appear to be under the div tag with class =
“quoteText”, and the author of the quote is under the span tag with class = “authorOrTitle” ,so I will
use this information to help me gather the data.
Furthermore, I can see that those two classes are under other div tags. So this information will help
me locate these two classes that contain the data that I want through the program. Also the site
itself has many pages with quotes, so this means I can iterate through each page to gather more
quotes simply by changing the page number at the end of the URL.
https://www.goodreads.com/quotes/tag/inspirational?page=2
If you prefer not to read this article and would like a video representation of it, you can check out the
YouTube Video . It goes through everything in this article with a little more detail, and will help make
it easy for you to start programming even if you don’t have the programming language Python
installed on your computer. Or you can use both as supplementary materials for learning.
Programming
First I will write a description about the program, this way I can simply read the description and know
what this program is about or is supposed to do.
Next, I want to import the libraries that are needed throughout the program
import requests
import urllib.request
import time
Now, create empty lists to store the inspirational quote and the author of the quote.
authors = []
quotes = []
Time for the “meat” of the program. I will create a function to automatically scrape the quote and
the author of the quote and store that data into the empty lists created previously.
def scrape_website(page_number):
for i in quoteText:
quote = i.text.strip().split('\n')[0]#Get the text of the current quote, but only the sentence before a
new line
#print(quote)
quotes.append(quote)
#print(author)
n = 10
scrape_website(num)
Combine the two lists together.uthors.append(author)
combined_list = []
for i in range(len(quotes)):
combined_list.append(quotes[i]+'-'+authors[i])
Finally, time to show the inspirational quotes and the author of that quote!
combined_list
That’s it, you are done! Hopefully this was useful to you!
If you are interested in reading more on Python one of the fastest growing programming languages
that many companies and computer science departments use, then I recommend you check out the
book Learning Python written by Mark Lutz’s.
Conclusion
Thanks for reading this article I hope its helpful to you all! If you enjoyed this article and found it
helpful please leave a comment to show your appreciation. Keep up the learning, and if you like
machine learning, mathematics, computer science, programming or algorithm analysis, please visit
and subscribe to my YouTube channels (randerson112358 & computer science).