ProxyTee
TutorialUpdated 2026-10-248 min readBy Alex Mercer, Lead Network Architect

Web Scraping with R: A Practical Guide Using ProxyTee

Web scraping is a powerful technique for extracting data from websites, and R provides excellent tools to do so. In this guide, we'll explore how to...

Web Scraping with R: A Practical Guide Using ProxyTee — ProxyTee

Web scraping is a powerful technique for extracting data from websites, and R provides excellent tools to do so. In this guide, we'll explore how to perform web scraping using R, focusing on practical applications and how ProxyTee can enhance your scraping efforts.

Getting Started with Web Scraping in R

Let's delve into the essential aspects of web scraping in R. This section provides an overview of necessary tools, environment setup, and fundamental concepts.

Understanding the Tools: R and rvest

R is a language and environment for statistical analysis and data visualization, ideal for wrangling and processing extracted data.

rvest is a popular R package for web scraping. Its intuitive interface makes extracting data straightforward from web pages, and you can easily extend it to scrape multiple pages by combining it with other packages.

Setting Up the Dev Environment

If you're new to R or RStudio, you should follow the installation instructions before proceeding.

To start, open the R console and install the rvest package with the command:

install.packages("rvest")

To fully leverage rvest’s capabilities, you can extend it with packages from the tidyverse collection like magrittr and xml2. You can do this by installing the whole tidyverse:

install.packages("tidyverse")
web scraping with c#

Understanding the Web Page

Web scraping involves retrieving data from websites through automated processes. Three core elements are important for web scraping:

  • Data appears in many forms.
  • Websites are structured differently.
  • Accessing scraped data must follow legal standards.

To scrape a web page, you need to understand the HTML structure and CSS styles it uses.

HTML and CSS Basics

HTML dictates content and layout via a Document Object Model (DOM), utilizing 'tags' to organize elements. Every tag, such as <html>, <head>, <body>, etc., encapsulates content using opening (<tag>) and closing (</tag>) statements. HTML tags have attributes like class or id which helps to define or to specify them. For example, you will find commonly <div> for document section and <p> for paragraph.

CSS controls web page styling, enabling you to alter colors, positions, and sizes. You may use inline styling within HTML using the style attribute, or use a standalone style sheet, applying a combination of tags, class names, or IDs as selectors.

Understanding DevTools

Web browsers include built-in developer tools which provide technical insights into web pages, enabling live information updates for debugging, testing, and performance analysis. This tutorial will reference Chrome's DevTools. In order to use it, just find developer tools inside the settings menu of Chrome, from there you will find an “elements” tab where the HTML structure of a webpage is. As you explore the structure, you can see the visual representation and the relationship of different parts. With the top-left icon, you can click on any area of the visual and it will directly navigate to it in the raw HTML view, also selecting its parent-child components. These features allow extracting of the required selectors for web scraping.

Deep-Dive into Web Scraping in R: Tutorial

In this practical section, we'll use a publicly available Amazon URL to demonstrate how to extract product reviews. We will get use of ProxyTee as a robust and reliable tool to help the extraction.

Prerequisites

Before beginning, ensure you have these installed:

  • R (>= 4.2.2)
  • rvest (>= 1.0.3)
  • tidyverse (>= 1.3.2)

Interactively Exploring the Web Page

Using Chrome's DevTools, examine the HTML to create selectors for data retrieval. For example, all customer reviews are found in a <div> that matches the format customer_review_$INTERNAL_ID.

From DevTools, locate elements of interest and copy the CSS selectors: review-title-content for the review titles, review-text-content for the content, and review-rating for star ratings. Each class is identifiable with the following CSS descriptors:

  • customer_review-R2U9LWUSIPY0GS > div:nth-child(2) > a.a-size-base.a-link-normal.review-title.a-color-base.review-title-content.a-text-bold > span for the title
  • customer_review-R2U9LWUSIPY0GS > div.a-row.a-spacing-small.review-data > span > div > div.a-expander-content.reviewText.review-text-content.a-expander-partial-collapse-content > span for the body
  • customer_review-R2U9LWUSIPY0GS > div:nth-child(2) > a:nth-child(1) > i.review-rating > span for the rating

CSS Selector vs. XPath for Web Scraping

For element identification, this guide uses CSS selectors. Another method is XPath, which identifies the element based on its path in the DOM. A CSS selector is faster and it could be more preferable. However, personal preference is a stronger guide to choose one or another, as both perform really similarly. You may copy Xpath following the same procedure in DevTools. For example, title's XPath can be defined as /html/body/div[2]/div[3]/div[6]/div[32]/div/div/div[2]/div/div[2]/span[2]/div/div/div[3]/div[3]/div/div[1]/div/div/div[2]/a[2]/span. You can select CSS selector or XPath, according to what better suits your programming workflow.

Programmatically Extracting Information from the Web Page

Let's write R scripts to programmatically extract information from the web page.

First, create a new script and load required libraries:

library("rvest")
library("tidyverse")

Next, set the URL, and extract ASIN from the url, use str_match() and also RegEx for cleaning.

HtmlLink <- "https://www.amazon.co.uk/Xbox-Elite-Wireless-Controller-2/dp/B07SR4R8K1/ref=sr_1_1_sspa?crid=3F4M36E0LDQF3"
ASIN <- str_match(HtmlLink, "/dp/([A-Za-z0-9]+)/")[,2]

Use read_html() to load the content of the HTML page, that comes as output of xml2 package.

HTMLContent <- read_html(HtmlLink)

You can extract different parts from this variable using html_nodes() and html_text() to get clean strings, using the CSS descriptors and removing customer specific identifiers, and “>“ connector to make code more efficient. So now, you can extract data from your three main selectors. Here you have code example for the titles.

review_title <- HTMLContent %>%
html_nodes("div:nth-child(2) a.a-size-base.a-link-normal.review-title.a-color-base.review-title-content.a-text-bold span") %>%
html_text()

Also do this with review's body content and rating:

review_body <- HTMLContent %>%
html_nodes("div.a-row.a-spacing-small.review-data span div div.a-expander-content.reviewText.review-text-content.a-expander-partial-collapse-content span") %>%
html_text()

review_rating <- HTMLContent %>%
html_nodes("div:nth-child(2) a:nth-child(1) i.review-rating span") %>%
html_text()

Now you can process and modify this variable to export numeric info only:

review_rating <- substr(review_rating, 1, 3) %>% as.integer()

It is time to export our scraped content inside a data frame object for analysis:

df <- tibble(review_title, review_body, review_rating)

Now, it’s good practice to create a reusable function to follow best coding standards and to make scalability easier, so, let’s wrap previous code into a function: scrape_amazon <- function(HtmlLink).

Scaling to Multiple URLs

You might want to scale to multiple URLs to get more robust results, here is how to scale this solution:

In order to extract a wide variety of data, such as all of the competitors’ products listed on Amazon, web crawling could be a great option to search all these pages automatically. Web crawling should be a good ally for this case.

For scalability, you can consider several different aspects to customize code behavior. You need to set technical requirements that match business needs, which should also be compliant with your current systems. For example, you may be considering following:

  • Real-time or batch process?
  • Output formats (JSON, NDJSON, CSV, or XLSX)?
  • Output targets (email, API, webhook, or cloud storage)?

To expand the capabilities of rvest, the polite package can manage and optimize scraping by generating session and implementing rate-limiting rules. Here's a short overview of 3 key functionalities.

  • bow() creates a session for URL, which works like asking for permission to a webpage.
  • scrape() downloads and stores HTML for specified URLs, with direct usage of rvest function html_nodes() and html_text() to directly retrieve your target data.
  • nod(), will update URL to navigate to the next page on a web page, saving code of redundant session creation.

polite provides a user-friendly method of scraping multiple web pages by adhering to each page’s robot.txt configurations.

Enhancing Web Scraping with ProxyTee

While R and rvest are excellent for web scraping, scaling up can present certain issues. ProxyTee provides several key benefits:

  • Unlimited Bandwidth: Scrape without worrying about overages, ideal for intensive scraping tasks. Access unlimited bandwidth and work without constrains.
  • Global IP Coverage: With over 20 million IP addresses worldwide, ProxyTee allows you to scrape websites as if from various locations. Global IP coverage is crucial to bypass regional blocks.
  • Multiple Protocol Support: Support for HTTP and SOCKS5 enables the tool's compatibility with diverse software. You can easily connect with diverse tool stacks with its multiple proxy protocol support.
  • Auto Rotation: Customize IP rotation between 3 to 60-minute intervals to reduce detection when scraping. Get dynamic IP with auto rotation.
  • API Integration: Leverage an API integration to easily automate tasks and expand automation possibilities.
  • Affordable Pricing: Affordable solutions that can reduce web scraping expenses by more than 50%, without impacting quality.

When choosing between implementing in-house or using a third party solution, you need to factor in key aspects, such as:

  • Data expertize.
  • Infrastructure for large-scale scraping
  • Compliance and data regulation guidelines.

Websites come with varied structure, this variability directly impacts the complexity required to scrape and maintain these solutions, where the needed programming and DevOps experience will be increasingly more demanding.

To build an robust solution with scalable features consider these steps:

  • Reduce bot detection implementing random delays between web scrapes, or using different user agents.
  • Use proxies and manage IP rotations, as provided by ProxyTee.
  • Use specific tools for advanced tasks, such as simulating user behavior using RSelenium.

Conclusion

Combining the power of R with rvest and tools like ProxyTee, web scraping can transform how you gather and analyze online data. With a clear and well set workflow you will be able to improve your performance when scraping with different types of websites, and make scaling simpler when considering data-heavy workloads. If you prefer to skip the complex infrastructure setup, you might consider exploring pre-built web scraping services that might offer templates and data handling capacity.

Scale Your Pipeline with ProxyTee Unlimited Residential Proxies

Whether you are building automated crawlers or localized data pipelines, ProxyTee Unlimited Residential Proxies provide 20,000,000+ real household IPs across 100+ global locations with configurable 3 to 60 minute auto-rotation, full HTTP and SOCKS5 protocol support, and flat-rate port pricing starting at $29/month with zero per-gigabyte bandwidth caps.

Tags:Web ScrapingData Extraction
AM
Alex Mercer • Lead Network & Web Scraping Architect
Technically Reviewed by Marcus Vance • Director of Proxy Infrastructure & Network Security

Principal infrastructure engineer at ProxyTee specializing in rotating residential proxy networks, SOCKS5/HTTP tunneling, TLS fingerprint mitigation, and distributed data extraction pipelines.