Web Scraping Using Beautiful Soup..!

Web Scraping Using Beautiful Soup..!


"Collection of Quality Data in these days is like finding a light of Hope in an endless tunnel."

How to collect or scrape data using Beautiful Soup in Python..?

To start a business or to do a project in any field, the most basic & fundamental requirement is the data. Now the major problem arises, how to gather bits and pieces of information to achieve our goal.

Hi, I’m Gyan Vardhan & will provide you the basic idea, how to scrape the data using Beautiful Soup in Python.

Step 1:- Choose the website you want to scrape.

Today I scrape the gold prices from the website "https://www.bullion-rates.com/gold/INR/2007-1-history.htm"

                                                     Image for post

                                                                                                            Sample Table Data

This is data we need to scrape.

 

Step 2:- Understand the website better in terms of their HTML tags. In a website all the information resides within the tags, we just need to look closely & find the exact tags in which our data are present. Let’s get started with finding the correct HTML tags. If you right click on the page & select inspect option then you can see all the HTML tags. But we need to find the exact tags so go to gold price table & do inspect.

                                                        Image for post

                                                                                                           Inspect the Elements

In above screenshot you can see the HTML tags we need. The data we need from the website are in table format so our 1st approach is to check, Is there any table in the HTML tags & then find its respective table rows & table data. So I already highlighted the tags we need to scrape the data.

 

Step 3:-

Python Code

Importing important libraries

                                                                                Image for post

                                                                                                            Required Libraries

We need these 3 libraries to run the code.

 

Step 4:-

Set the url of the website & access it through requests library.

                                                                        Image for post

                                                                                                                  URL Setup

If the requests get passed or successful then response will be 200; check below images. 

                                                                                                         Image for post

                                                                                                      Check the Response Code

Now, (a) we have to write the code to find the table in which our data resides, there might be more than one table in the website, so in that case we have to give the class of the table to identify the exact table. In our case class of the table is “Grid History”. We use find() function & it will return our desired table.

Code will be like:-

                                                                                  Image for post

(b) Identify the rows of the table:- Tag for the row is “tr”. To get the row we will use find_all() function, it is just like find() function but it will give all the matching results.

Code will be like:-

                                                                                             Image for post

(C) Finally, we reached our last destination & Now, we have to find the body which resides in tag “td”. And to get all the data of all row, the code will be same but we need to loop the code to extract all the information at once.

Code will be like:-

                                                                                                Image for post

Our Final Scraped Data

                                                              Image for post

                                                                                                                       Scraped Data

Conclusion:- Web Scraping is an amazing tool, that makes anyone’s life simpler.

Find full code on

Find Full Code on Github

If you have any questions about the code or web scraping in general, reach out to me on

Connect to me on Linkedin

Stay tuned guys, for more updates.

How do you rate this article?

4


Gyan Vardhan
Gyan Vardhan

I’m a Data Science personnel, want to contribute and learn more.


Web Scraping Using Beautiful Soup..!
Web Scraping Using Beautiful Soup..!

How to collect or scrape data using Beautiful Soup in Python..? To start a business or to do a project in any field, the most basic & fundamental requirement is the data. Now the major problem arises, how to gather bits and pieces of information to achieve our goal.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.