How to match image urls with identical addresses except for image size and only retain one url for download?

Viewed 26

I have a df full of urls and image urls within the parent url. My webscraping script downloads all of the image urls. As you can see image urls 2-8 are the same picture but different dimensions/sizes. I am looking to keep one of the images so 6 versions of the photo are not downloaded. It doesnt matter which size is kept as long as there is only one of them.

This is also a part of a larger dataset and performed weekly. There are about 1500 image urls per week so the structure of the code has to be generic/standardized. I was thinking about doing the max length of the urls grouped by the article url, and matching starts with and ends with phrases but I feel that that isnt the best way and can lead to missing some pictures on sites with shorter urls.

structure(list(image_url = c("https://cloudfront-eu-central-1.images.arcpublishing.com/rtl/HDS4PPYVRD7SEHYOS6N6TFAROU.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/1024x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/290x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/345x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/395x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/728x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/399x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg", 
"https://ais-akamai.rtl.de/masters/1769218/527x0/lewis-hamilton-trennen-nur-noch-acht-punkte-von-max-verstappen.jpg"
), URL = c("https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html", 
"https://www.rtl.de/cms/heisser-wuestenendspurt-live-bei-rtl-faellt-am-sonntag-die-formel-1-entscheidung-4875243.html"
)), row.names = 1:8, class = "data.frame")
1 Answers

As long as the duplicate images have the same name after the last forward slash (/) the following code should work.

It adds a new column pic that has the characters from the last slash (/) to the end of img_url, groups them on the pic column, and selects (slices) only the first one.


library(dplyr)
library(stringr)

x %>% 
  mutate(pic = str_extract(image_url, "[^//]+$")) %>%  
  group_by(pic) %>% 
  slice(1) %>%
  ungroup()
#> # A tibble: 2 x 3
#>   image_url                     URL                       pic                   
#>   <chr>                         <chr>                     <chr>                 
#> 1 https://cloudfront-eu-centra… https://www.rtl.de/cms/h… HDS4PPYVRD7SEHYOS6N6T…
#> 2 https://ais-akamai.rtl.de/ma… https://www.rtl.de/cms/h… lewis-hamilton-trenne…

Created on 2022-03-10 by the reprex package (v0.3.0)

Related