Web scraping using python
Web scraping is one of the important Data analysis tool through which many valuable information in a web page can be obtained and processed.In this blog,we are going to scrape IMDb web page and we are going to store the useful data in an excel sheet using Python.The library we are going to use for scarping will be BeautifulSoup.We also use request library to get the webpage using the url of the page.We use openpyxl for storing the data in an excel sheet.Let’s get started.
First of all,we are going to install the required libraries,
>>pip3 install Beautifulsoup4
>>pip3 install request
>>pip3 install openpyxl
import the above libraries
Now,we are going to get the webpage through request.get()method.To get that in the form of text,use .text in the end.
The output of the above code is
Now,we are going to parse the page in html format.
Now,Go to the corresponding page and inspect the page.
Here,web scarping is done with the help of html tags.Every section in a web page has a html tag and we are going to get the useful data through the tag using the class name of the tag or id or using other selector names.To do so,we have to inspect the page.
Right click on the specific area where the useful information is displayed and click inspect to get the html tag along with the name of the class.
Here,we are going to get the top 100 film’s details.Here,one div is repeated hundred times to display the details of 100 films.The information that posses may differ but the tag remains the same.So,we are going to find the outter div tag for those 100 films and will use a for loop to iterate through 100 tags.
To get the outer tag,use find() method in Beautifulsoup,This function will remain the tag that comes first in the html page.
A part of output of the above code is given for your reference
Here,we specify the html tag along with the class name.As the inner tags are repeatative and we have to get all the tags,we can use findall() method,This will return a list with all the tags that matches the findall() parameters
Now,To get the details of all the 100 films,we use for loop with the each in which the tags of 100 films are stored as a list.
Get Grind.Coding’s stories in your inbox
Join Medium for free to get updates from this writer.
A part of the list is given
We are going to add the data in a excel sheet.Thus ,we have to create an excel sheet.
We create an excel workbook and activate an excel sheet.Then give it a title and append the column names in a list format.We are going to add the film’s details in the similar way.
The individual tags will be
As they are repeatative,we can scrape a single instance and use a for loop to scarpe the other.
Go deep into the tags to get information
In this code,In the <a> tag,we can find the movie’s name,So here we can just access the <a> tag for all 100 movies..
The output will be
use .text to get the text data of the tag
Likewise,to get year,rating and rank,handle the tags in a proper way
To get the rank,
To get the rating and the year of release,do the same.The entire for loop will be
For all the data,we find the specific tag.Similarly,we can get all the details through the tags and store them in a variable.At last,we are going to append them in the excel sheet.That’s all,a simple and efficient web scraping is done and dusted.
Save the excel file at the end
The output will be
The entire code is given for your reference
Here,exception handling is used to catch the exception if the link is expired.






