Reader small image

You're reading from  R Web Scraping Quick Start Guide

Product typeBook
Published inOct 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789138733
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Olgun Aydin
Olgun Aydin
author image
Olgun Aydin

Olgun Aydin is a PhD candidate at the Department of Statistics at Mimar Sinan University, and is studying deep learning for his thesis. He also works as a data scientist. Olgun is familiar with big data technologies, such as Hadoop and Spark, and is a very big fan of R. He has already published academic papers about the application of statistics, machine learning, and deep learning. He loves statistics, and loves to investigate new methods and share his experience with other people.
Read more about Olgun Aydin

Right arrow

Web Scraping with rvest

All the data we need today is already available on the internet, which is great news for data scientists. The only barrier to using this data is the ability to access it. There are some platforms that even include APIs (such as Twitter) that support data collection from web pages, but it is not possible to crawl most web pages using this advantage.

Before we go on to scrape the web with R, we need to specify that this is advanced data analysis, data collection. We will use the Hadley Wickham's method for web scraping using rvest. The package also requires selectr and xml2 packages.

The way to operate the rvest pole is simple and straightforward. Just as we first made web pages manually, the rvest package defines the web page link as the first step. After that, appropriate labels have to be defined. The HTML language edits content using various tags...

Introducing rvest

Most of the data on the web is in large scale as HTML. It is often not available in a form that is useful for analysis, such as hierarchical or tree-based:

<html>
<head>
<title>Looks like a tittle</title>
</head>
<body>
<p align="center">What's up ?</p>
</body>
</html>

rvest is a very useful R library that helps you collect information from web pages. It is designed to work with magrittr, inspired by libraries such as BeatifulSoup.

To start the web scraping process, you first need to master the R bases. In this section, we will perform web scraping step by step, using the rvest R package written by Hadley Wickham.

For more information about the rvesr package, visit the following URLs.CRAN Page: https://cran.r-project.org/web/packages/rvest/index.html rvest on github: https://github.com...

Step-by-step web scraping with rvest

After talking about the fundamentals of the rvest library, now we are going to deep dive into web scraping with rvest. We are going to talk about how to collect URLs from the website we would like to scrape.

We will use some simple regex rules for this issue. As we have learned how XPath works, then its time to write XPath rules. Once we have XPath rules and regex rules ready, we will jump into writing scripts to collect data from the website. That would be great, if we have a chance to play with the data we are going to collect. Don't worry; we will play with data, draw some plots, and create some charts.

We will collect a dataset from a blog, which is about big data (www.devveri.com). This website provides useful information about big data, data science domains. It is totally free of charge. People can visit this website and find use...

Summary

In this chapter, we have learned how to write a scraping script using the rvest library. First, we worked on how to collect URLs, and then we worked on how to create XPath rules. After that, we wrote our first web scraping script using the rvest library. We have applied basic text and manipulation techniques. Once we had the final data ready, we calculated the daily average comment counts, and at the final step, we created a line plot using basic R plots and plotly plots to visualize the average comment count, article counts for each categories, and the summary statistics of the comment counts.

In the next chapter, we will talk about the fundamentals of Selenium and how to do web scraping using the RSelenium library. At the end of the next chapter, we will be writing R script to collect data from the web using Selenium.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
R Web Scraping Quick Start Guide
Published in: Oct 2018Publisher: PacktISBN-13: 9781789138733
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Olgun Aydin

Olgun Aydin is a PhD candidate at the Department of Statistics at Mimar Sinan University, and is studying deep learning for his thesis. He also works as a data scientist. Olgun is familiar with big data technologies, such as Hadoop and Spark, and is a very big fan of R. He has already published academic papers about the application of statistics, machine learning, and deep learning. He loves statistics, and loves to investigate new methods and share his experience with other people.
Read more about Olgun Aydin