Code
library(tidyverse)
library(knitr)Assignment 7: Ethical Web Scraping
library(tidyverse)
library(knitr)When people shop online, they often treat a higher price as a sign of a better product, and a star rating as a summary of how happy buyers were. This project asks whether those two signals line up in an online bookstore:
Do books that cost more get higher star ratings, and does this differ by genre?
This matters for business analytics because a store that assumes “premium” books are rated more favorably might price, promote, and recommend books based on that assumption. If price and rating are unrelated, price is a poor shortcut for quality. I split the question into three parts:
Note: Books to Scrape is a practice bookstore built for learning web scraping, so its prices and ratings are not real market data. The results describe this site, not the real book market.
Data source. The home page of books.toscrape.com lists about 1,000 books in 50 categories. Every book card shows the title, price, and star rating, and each category page gives the genre. That is everything I need to study price, rating, and genre, and it is well above the required 100 rows and 5 variables.
How I collect it. The scraping code is in a separate script, scrape_books.R. It has three functions:
get_page() downloads one page, with my user agent and a 3 second wait.scrape_page() pulls the title, price, and rating of every book on one page.scrape_books() uses a for loop over all categories, and a second for loop over the pages inside each category.The result is saved as books_scraped.csv, which I host in the cloud and import below.
Ethical scraping choices.
| Practice | What I did |
|---|---|
| Use an API if one exists | The site has no API, so I scraped the HTML |
| Create new value, not duplicate | I analyze relationships between price, rating, and genre instead of republishing the catalog |
| Identifying user agent | My user agent names me, my school, my purpose, and my email |
| Reasonable request rate | The script waits 3 seconds before every request |
| Only scrape what is needed | I requested only the category listing pages (about 80 requests) and skipped the 1,000 individual book pages |
| Robots rules | I read the site’s robots.txt before scraping |
| Cite sources | See the Sources section at the end |
I scraped the data once, saved it as a CSV, and uploaded it to the cloud. Here I import it from that link.
data_url <- "https://myxavier-my.sharepoint.com/:x:/g/personal/adhiamboj_xavier_edu/IQBnnXgnHUzISbFymz8vNukwAWlocb2vFWJ0xbKBlFdnvuo?e=oyiOJB&download=1"
books <- read_csv(data_url)The data has 1000 books and 6 variables. For wrangling, I made category a factor and rating a whole number, and removed any duplicate rows.
books <- books %>%
distinct() %>%
mutate(category = as.factor(category),
rating = as.integer(rating))
head(books) %>% kable()| title | category | price_gbp | rating | page_url | scraped_at |
|---|---|---|---|---|---|
| It’s Only the Himalayas | Travel | 45.17 | 2 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
| Full Moon over Noah’s Ark: An Odyssey to Mount Ararat and Beyond | Travel | 49.43 | 4 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
| See America: A Celebration of Our National Parks & Treasured Sites | Travel | 48.87 | 3 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
| Vagabonding: An Uncommon Guide to the Art of Long-Term World Travel | Travel | 36.94 | 2 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
| Under the Tuscan Sun | Travel | 37.33 | 3 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
| A Summer In Europe | Travel | 44.34 | 2 | https://books.toscrape.com/catalogue/category/books/travel_2/index.html | 2026-11-23 23:06:41 |
books %>%
summarise(books = n(),
categories = n_distinct(category),
mean_price = round(mean(price_gbp), 2),
median_price = round(median(price_gbp), 2),
min_price = min(price_gbp),
max_price = max(price_gbp),
mean_rating = round(mean(rating), 2)) %>%
kable(caption = "Table 1. Summary of the scraped data (prices in £)")| books | categories | mean_price | median_price | min_price | max_price | mean_rating |
|---|---|---|---|---|---|---|
| 1000 | 50 | 35.07 | 35.98 | 10 | 59.99 | 2.92 |
ggplot(books, aes(x = price_gbp)) +
geom_histogram(binwidth = 2.5, fill = "steelblue", color = "white") +
labs(title = "Figure 1. Distribution of book prices",
x = "Price (£)", y = "Number of books")ggplot(books, aes(x = factor(rating))) +
geom_bar(fill = "steelblue") +
labs(title = "Figure 2. Distribution of star ratings",
x = "Star rating", y = "Number of books")Prices in Figure 1 run from £10 to £59.99, with a typical (median) price of £35.98. Figure 2 shows how many books earned each star rating; the average rating is 2.92. Both variables have enough spread to look for a relationship.
category_summary <- books %>%
group_by(category) %>%
summarise(books = n(),
mean_price = round(mean(price_gbp), 2),
mean_rating = round(mean(rating), 2)) %>%
filter(books >= 10) %>%
arrange(desc(mean_price))
head(category_summary, 10) %>%
kable(caption = "Table 2. The 10 categories with the highest average price (categories with 10 or more books)")| category | books | mean_price | mean_rating |
|---|---|---|---|
| Travel | 11 | 39.79 | 2.73 |
| Fantasy | 48 | 39.59 | 3.08 |
| History | 18 | 37.30 | 2.94 |
| Womens Fiction | 17 | 36.79 | 3.12 |
| Classics | 19 | 36.55 | 2.47 |
| Fiction | 65 | 36.07 | 3.18 |
| Poetry | 19 | 35.97 | 3.53 |
| Horror | 17 | 35.95 | 2.71 |
| Add a comment | 67 | 35.80 | 2.76 |
| Music | 13 | 35.64 | 3.15 |
ggplot(category_summary, aes(x = reorder(category, mean_price), y = mean_price)) +
geom_col(fill = "steelblue") +
coord_flip() +
labs(title = "Figure 5. Average price by category",
x = NULL, y = "Average price (£)")anova_price <- summary(aov(price_gbp ~ category, data = books))
anova_rating <- summary(aov(rating ~ category, data = books))
p_price <- anova_price[[1]][["Pr(>F)"]][1]
p_rating <- anova_rating[[1]][["Pr(>F)"]][1]
anova_price Df Sum Sq Mean Sq F value Pr(>F)
category 49 10000 204.1 0.977 0.52
Residuals 950 198498 208.9
anova_rating Df Sum Sq Mean Sq F value Pr(>F)
category 49 114.2 2.331 1.14 0.24
Residuals 950 1942.8 2.045
Interpretation. I ran two one-way ANOVA tests, one for price and one for rating, to see if the averages differ across categories.
Overall, the evidence does not support the idea that a higher price signals a higher customer rating in this store. For a business, this suggests price alone would be a poor stand-in for quality.
Limitations. Books to Scrape is a practice site, so the findings should not be applied to real bookstores. The data is one snapshot with no information on how many people rated each book. The analysis shows association, not cause.
scrape_books.R.rvest (scraping); tidyverse and knitr (analysis).