Web Scraping And Analyzing Data Using R

To make you realize the importance of web scraping,  I will take you through one example that happened to me in the recent past. One fine day one of my Chinese friends called me to discuss our university MoU norms. But as I was not in the home and my mother received the call but as she was not able to understand his language, as a result, I was not able to help him. He misunderstands me, then I realize the necessity of a translator in human life.

If I would have been present at home, I could have translated the information delivered in Chinese and then interpreted and clarify his concerns. On a similar note, web scrapping plays the same roll in the present online, social and the digital world.

In websites and other online sources, different types of commodities information are available but sometimes we cannot use that information for analysis and then draw valuable insights from the raw data. So web scraping comes to rescue here. It becomes necessary to scrap or to read the ungrouped data in a grouped format, as a result, we make graphical views using the available information and comparisons on different websites.

Now we will discuss what the web scrapping is. Web scraping is a technique for converting the data in an unstructured format with HTML tags from the web to the structured format which can easily be accessed, used and analyzed.

Why we need a Web Scrapping?

This is because it is not possible to collect information physically. But we can get the information easily by internet surveys or through social media. So, it is necessary to access the data from the internet.

Some applications of web scrapping in real life:

  • For the purpose of comparison between different movies, medicines etc. to analyze the data
  • For the purpose of scraping an image from different websites to train image classification
  • For the purpose of scrapping data from social media sites such as Facebook, Twitter for sentiment analysis
  • Scrapping user reviews and feedbacks from Flipkart and Amazon etc. for the purpose of e-commerce
  • To know which laptop is better among HP, DELL etc. we use different reviews and the web scrapping method to compare the data
  • Web scraping is also necessary for the purposes of reviewing which bus is better for transportation or which hotel is cheap for a tourist

Different Types of Web Scrapping:

Human Copy-Paste: This is a slow and efficient way of scraping data from the web. This involves humans for copy-paste the data from different websites.

Text pattern matching: Another simple yet powerful approach to extract information from the web is by using regular expression matching facilities of programming languages.

API Interface: Many websites like Facebook, Twitter, LinkedIn, etc. provides public and/ or private APIs which can be called using the standard code for retrieving the data in the prescribed format.

DOM Parsing: By using the web browsers, programs can retrieve the dynamic content generated by client-side scripts.

There are many software for web scraping. Here we will discuss how we can scrap the data from a website using R and R Studio software.

Suppose we search “mobile” in flipkart.com and have search results. Now, I want to collect the data of the mobiles from that page. Suppose we want the name, price, and ratings of those mobiles. Now first we have to copy the link of that webpage which I want to scrape and then follow the r codes,

Running the above codes we get the following output:

Now, one should ask what is the second element in the function html_nodes(). The second element is the “CSS” which we have to select. You may think of it as an address of the elements which you to select in the webpage. Now you can find these “CSS” by the following method –

  1. Install “selector gadget” extension to your Google Chrome.
  2. Now, go to the webpage.
  3. Then click on the extension.
  4. Now, just select the one you are interested in.
  5. You will get the CSS selector in a tab below.

You can follow the visit https://selectorgadget.com/ to understand how “selector gadget” works.

Another question that may arise is why I have used some replacement for the variables “price” and “rating”. Basically, when we scrap a data from the webpage it may not be saved as we need. So, we have to modify it to serve our purpose.

So, this is how we easily scarp a data from a webpage using R. After getting these we can analyze the data as our need.



You might also like More from author

Leave A Reply

Your email address will not be published.