WebScraping With Python, Beautiful Soup, and Urllib3

WebScraping With Python, Beautiful Soup, and Urllib3

WebScraping With Python, Beautiful Soup, and Urllib3 - Easily scrape websites with Python and Beautiful Soup.

Originally published by Leaundrae Mckinney  at dzone.com

In this day and age, information is key. Through the internet, we have an unlimited amount of information and data at our disposal. The problem, however, is because of the abundance of information we as the users become overwhelmed. Fortunately, for those users, there are programmers with the ability to develop scripts that will do the sorting, organizing, and extracting of this data for them. Work that would take hours to complete can be accomplished with just over 50 lines of code and run in under a minute. Today, using Python, Beautiful Soup, and Urllib3, we will do a little WebScraping and even scratch the surface of data extraction to an excel document.

Research

The website that we will be working with is called books.toscrape.com. It's one of those websites that is literally made for practicing WebScraping. Before we begin, please understand that we won't be rotating our IP Addresses or User Agents. However, on other websites, this may be a good idea, since they will most likely block you if you're not "polite." (I'll talk more on the concept of being polite in later posts. For now, just know that it means to space out the amount of time between your individual scrapes.) 

Okay, let's take a look at our target.

Basically, we want a list of every book title and price from this website. We notice that the prices are in British Pounds, so we'll want to convert them into US Dollars. If we scroll to the bottom of the page, we notice that there are 50 pages worth of books. Therefore, our script will have to iterate 50 times, while altering the base URL each time. The URL for this page changes one number each time, so a simple for loop should do the trick.

Setup, Urllib3, and Beautiful Soup

Here's a breakdown of our tasks:

  • Import the required modules and create two master lists (titles and prices).
  • Using Urllib3 and Beautiful Soup, set up the environment to parse the first page.
  • Collect every book title from the page, and append it to one of the master lists.
  • Collect every book price from the page, convert to USD, and append to the prices master list.
  • Convert both master lists into a single dictionary.
  • Export to a CSV.

Now that we have our outline, we can get to work. Since we'll be putting everything into a function, be mindful of your indentations. Let's begin!

First, let's import our modules and define our function.

import urllib3, re
from bs4 import BeautifulSoup
from csv import DictReader, DictWriter
#The file name will be whatever you decide when running the function
def get_book_data(filename):
    #These will be our Master Lists and must remain outside of any loops
    titles = []
    prices = []

Urllib3 is an HTTP Client for Python. It's pretty versatile and perfect for what we need. For more information, check out the docs. Throughout most of your Web Scraping, there will be a time that Regex typically comes in handy. A prime example of this is that all of the prices on the page have a pound symbol in front of the numbers. One of the easiest ways to remove and replace the symbol is through Regular Expressions. Finally, since we want to write our information to a CSV via a dictionary, it only makes sense to use the CSV module.

Next, we define our function as getbookdata and pass in the argument filename that we will choose for our CSV.

#Convert British Pounds to USD (as of 20190801)
def gbp_to_usd(amount):
    return f'$ {round((amount * 1.21255), 2}'

As of August 1st, the conversion rate from British Pounds to US dollars is 1.21255. By defining this function, we are able to call it later when the time comes. Then, by wrapping our calculations in the round method, we can round the number to the hundredths place.

So far so good now let's get into it.

#Prepare to scrape all 50 pages
for i in range(1,51):
    #All of the page URLs follow the same format with the exception of one number followed by 'page-'
    url = f'http://books.toscrape.com/catalogue/category/books_1/page-{i}.html'
    req = urllib3.PoolManager()
    res = req.request('GET', url)
    soup = BeautifulSoup(res.data, 'html.parser')
    contents = soup.find_all(class_= 'product_pod')

Because there are 50 pages, our range will need to be from 1 to 51 in order to capture all of them. Our URL takes us to the first page. Throughout each iteration, one will be added to i, giving us a new URL each time.

The PoolManager method allows for arbitrary requests while transparently keeping track of connection pools for us. The type of request that we are initiating to our URL is a GET request, which means that all we want is data.

Using Beautiful Soup, we pull all of the data from our request, specifically the HTML data. If we take a look at the source code on our webpage, we'll notice that all of the products fall under the class productpod. By calling the findall method, we request all of the HTML with the class of product_pod.

#Based off of the title parameter within the site html
        for i in soup.find_all():
            titles.append(i['title'])

If we look once again at the source data, we notice that the title appears twice — once as the inner text and the other as the value for the parameter title. Normally, we would extract from the inner HTML, but since the inner text cuts off most of the title we have to extract from within a tag.

#Temporary lists for British currency conversions
        pounds = []
        c = []
        for i in contents:
            c.append(i.find().get_text())
            for number in c:
                #Extract the British Pound symbol and join the numbers and decimal points back together
                amount = re.compile('[0-9]+.')
                num = amount.findall(number)
                pounds.append(float(''.join(num)))

Just like we did for the title, we are going to extract all of the HTML that falls within the price_color class. Now, remember that all of the prices are in British Pounds; so we need to remove the Pound symbol and replace it with the dollar sign. Before that, we extract all of the numbers and decimals from the gathered prices using regex. Once we have a list of numbers and decimals, we join and then append the floats to our temporary pounds list.

#Create a temporary list for the current loop and append to the master list after we run the conversion function
	temp = list(map(gbp_to_usd_rounded,pounds))
    for t in temp:
		prices.append(t)
    #Combine both lists into a dictionary
    res = dict(zip(titles,prices))

Again, we create another temporary list and call our conversion function on the Pounds list we just created. Finally, we run a quick for loop and append those items to our master prices list.

Now that we have both master lists, we're able to create a dictionary using the zip method. Make sure that this is outside of any loops; otherwise, it won't work as intended.

Conclusion

We open the specified filename with the intent to write. With Python 3 when we are web scraping, we have to include the argument otherwise the spreadsheet will have blank lines between each entry. We run a simple iteration through the dictionary ensuring that our keys match the headers that we specified. Finally we execute getbookdata with our file title and extension, wait about 20-30 seconds and boom we have an excel document with all of the titles and prices from our target website!

#Create an Excel Document with the dictionary
    with open(filename,'w',) as file:
        headers = ("Book Title", 'Price (in usd)')
        csv_writer = DictWriter(file,)
        csv_writer.writeheader()
        for k,v in res.items():
            csv_writer.writerow({
                'Book Title' : k,
                'Price (in usd)' : v
        })
get_book_data('BookResearch_Beta.csv')

If you found value in this article, please share and leave a comment below. Let me know what projects you are working or would like to see in the future. You can find all of the source code on my GitHub. Until next time!

Originally published by Leaundrae Mckinney  at dzone.com

===================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Complete Python Bootcamp: Go from zero to hero in Python 3

☞ Python and Django Full Stack Web Developer Bootcamp

☞ Python for Time Series Data Analysis

☞ Python Programming For Beginners From Scratch

☞ Beginner’s guide on Python: Learn python from scratch! (New)

☞ Python for Beginners: Complete Python Programming

☞ Django 2.1 & Python | The Ultimate Web Development Bootcamp

☞ Python eCommerce | Build a Django eCommerce Web Application

☞ Python Django Dev To Deployment

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Hire Now: https://bit.ly/394wdOx

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company