Scraping demo

In this article we are going to scrap all new car ads from local car sale announcment website turbo.az Firstly, we load libraries

library(rvest, warn.conflicts=F, quietly=T)
library(dplyr, warn.conflicts=F, quietly=T)

After, we attach the website link via read_html() function. Then by means of html_node() and html_text() functions we will get necessary nodes from html page. To get html tag of those nodes we can use “Inspect code” or SelectorGadget extension.

link="https://turbo.az/autos"
page=read_html(link)

car_name=page %>% html_nodes("div.products-i__name.products-i__bottom-text") %>% html_text()
car_info=page %>% html_nodes("div.products-i__attributes.products-i__bottom-text") %>%html_text()
car_price=page%>%html_nodes("div.product-price")%>%html_text()
ad_time=page %>% html_nodes("div.products-i__datetime") %>% html_text()
#View(car_name)

II step: Exporting result

Now, when we draw out all necessary data, it is time to export our results to .csv (json, txt) file. First of all lets create frame to combine all vectors into one.

car_data=data.frame(car_name, car_info,car_price, ad_time)
#Before export you can set your working directory
#write.csv(car_data,"car_data.csv")   #Export to .csv file
head(car_data)
##           car_name                car_info  car_price                ad_time
## 1       Polestar 2       2022, 0.0 L, 0 km   44 600 $      Bakı, dünən 14:20
## 2    Jeep Wrangler       2021, 2.0 L, 0 km   71 400 $ Bakı, 12.10.2022 10:58
## 3           BMW X5       2022, 2.0 L, 0 km   94 500 $ Bakı, 05.10.2022 11:42
## 4       Kia Optima  2013, 2.0 L, 79 396 km 28 400 AZN Bakı, 13.10.2022 17:06
## 5 Hyundai Grandeur 2009, 2.7 L, 241 000 km 18 500 AZN Bakı, 10.10.2022 16:53
## 6        Ford Edge 2014, 3.5 L, 300 000 km 23 000 AZN Bakı, 12.10.2022 10:58

III step: Getting more car information

In this step we are going to fetch more detailed info about the cars. For that aim we have to get the link to the car’s detailed info page.
Add this code line to above code block.

car_link=page %>% html_nodes("a.products-i__link") %>% html_attr("href") %>% paste("https://turbo.az",.,sep="")
#And change data.frame 
car_data=data.frame(car_name, car_info,car_price, ad_time, car_link, stringsAsFactors = FALSE)
head(car_data)
##           car_name                car_info  car_price                ad_time
## 1       Polestar 2       2022, 0.0 L, 0 km   44 600 $      Bakı, dünən 14:20
## 2    Jeep Wrangler       2021, 2.0 L, 0 km   71 400 $ Bakı, 12.10.2022 10:58
## 3           BMW X5       2022, 2.0 L, 0 km   94 500 $ Bakı, 05.10.2022 11:42
## 4       Kia Optima  2013, 2.0 L, 79 396 km 28 400 AZN Bakı, 13.10.2022 17:06
## 5 Hyundai Grandeur 2009, 2.7 L, 241 000 km 18 500 AZN Bakı, 10.10.2022 16:53
## 6        Ford Edge 2014, 3.5 L, 300 000 km 23 000 AZN Bakı, 12.10.2022 10:58
##                                          car_link
## 1       https://turbo.az/autos/6594039-polestar-2
## 2    https://turbo.az/autos/5799964-jeep-wrangler
## 3           https://turbo.az/autos/6501730-bmw-x5
## 4       https://turbo.az/autos/6718751-kia-optima
## 5 https://turbo.az/autos/6702291-hyundai-grandeur
## 6        https://turbo.az/autos/6646173-ford-edge

Now as we get car detailed information page link, lets create a function, which will fetch data from this link.

get_moreInfo_func=function(car_link){
  car_link="https://turbo.az/autos/6550523-fiat-doblo"
  moreInfo_page=read_html(car_link)
  moreInfo = moreInfo_page %>% html_nodes(".product-properties")%>%html_text() %>% paste(collapse ="," )
  return(moreInfo)
}  #End of function
#View(moreInfo)

Lets test this function and after replace in code block.

library(rvest)
library(dplyr)

link="https://turbo.az/autos"
page=read_html(link)

car_name=page %>% html_nodes("div.products-i__name.products-i__bottom-text") %>% html_text()
car_info=page %>% html_nodes("div.products-i__attributes.products-i__bottom-text") %>%html_text()
car_price=page%>%html_nodes("div.product-price")%>%html_text()
ad_time=page %>% html_nodes("div.products-i__datetime") %>% html_text()
car_links=page %>% html_nodes("a.products-i__link") %>% html_attr("href") %>% paste("https://turbo.az",.,sep="")

get_moreInfo_func=function(car_link){
  moreInfo_page=read_html(car_link)
  moreInfo=moreInfo_page %>% html_nodes(".product-properties")%>%html_text() %>% paste(collapse ="," )
  return(moreInfo)
}

car_moreInfo=sapply(car_links, FUN=get_moreInfo_func)

car_data=data.frame(car_name, car_info,car_price, ad_time, car_moreInfo, stringsAsFactors = FALSE)
row.names(car_data)<-NULL

#write.csv(car_data,"car_data.csv")
head(car_data)
##                 car_name                car_info  car_price
## 1           Nissan Tiida  2012, 1.5 L, 46 000 km 14 600 AZN
## 2           Lexus LX 570  2013, 5.7 L, 83 000 km   61 000 $
## 3       Bentley Bentayga  2017, 6.0 L, 46 820 km  199 900 $
## 4      ZX Auto Terralord       2022, 2.5 L, 0 km 45 900 AZN
## 5 Land Rover Range Rover  2013, 3.0 L, 65 500 km   53 000 $
## 6         Mercedes E 270 2000, 2.7 L, 350 000 km 13 600 AZN
##                  ad_time
## 1      Bakı, dünən 10:34
## 2 Bakı, 09.10.2022 10:58
## 3 Bakı, 13.10.2022 13:11
## 4 Bakı, 10.10.2022 10:18
## 5 Bakı, 06.10.2022 23:15
## 6 Bakı, 14.10.2022 12:19
##                                                                                                                                                                                                                                                                                                                car_moreInfo
## 1 ŞəhərBakıMarkaNissanModelTiidaBuraxılış ili2012Ban növüHetçbekRəngGümüşüMühərrik1.5 LMühərrikin gücü109 a.g.Yanacaq növüBenzinYürüş46 000 kmSürətlər qutusuAvtomatÖtürücüÖnYeniXeyrQiymət14 600 AZNKreditdədirBarter mümkündürHansı bazar üçün yığılıbYaponiyaYerlərin sayı5Sahiblər1VəziyyətiVuruğu yoxdur, rənglənməyib
## 2      ŞəhərBakıMarkaLexusModelLX 570Buraxılış ili2013Ban növüOffroader / SUVRəngAğMühərrik5.7 LMühərrikin gücü383 a.g.Yanacaq növüBenzinYürüş83 000 kmSürətlər qutusuAvtomatÖtürücüTamYeniXeyrQiymət61 000 $Barter mümkündürHansı bazar üçün yığılıbRəsmi dilerYerlərin sayı7Sahiblər1VəziyyətiVuruğu yoxdur, rənglənməyib
## 3                                                                                                             ŞəhərBakıMarkaBentleyModelBentaygaBuraxılış ili2017Ban növüOffroader / SUVRəngQaraMühərrik6.0 LMühərrikin gücü608 a.g.Yanacaq növüBenzinYürüş46 820 kmSürətlər qutusuAvtomatÖtürücüTamYeniXeyrQiymət199 900 $
## 4                                                          ŞəhərBakıMarkaZX AutoModelTerralordBuraxılış ili2022Ban növüPikapRəngAğMühərrik2.5 LMühərrikin gücü146 a.g.Yanacaq növüDizelYürüş0 kmSürətlər qutusuMexanikiÖtürücüTamYeniBəliQiymət45 900 AZNHansı bazar üçün yığılıbAvropaVəziyyətiVuruğu yoxdur, rənglənməyib
## 5               ŞəhərBakıMarkaLand RoverModelRange RoverBuraxılış ili2013Ban növüOffroader / SUVRəngGöyMühərrik3.0 LMühərrikin gücü340 a.g.Yanacaq növüBenzinYürüş65 500 kmSürətlər qutusuAvtomatÖtürücüTamYeniXeyrQiymət53 000 $Hansı bazar üçün yığılıbAmerikaYerlərin sayı5Sahiblər2VəziyyətiVuruğu yoxdur, rənglənməyib
## 6                               ŞəhərBakıMarkaMercedesModelE 270Buraxılış ili2000Ban növüSedanRəngGöyMühərrik2.7 LMühərrikin gücü170 a.g.Yanacaq növüDizelYürüş350 000 kmSürətlər qutusuAvtomatÖtürücüArxaYeniXeyrQiymət13 600 AZNHansı bazar üçün yığılıbAvropaYerlərin sayı5Sahiblər2VəziyyətiVuruğu yoxdur, rənglənməyib

IV step: Getting all data from next pages

Let’s inspect page URL, when we go to the next page. It changes with some rules: https://turbo.az/autos?page=2 To fetch data from next pages we are going to make a loop (for, while)

library(rvest)
library(dplyr)

get_moreInfo_func=function(car_link){
  moreInfo_page=read_html(car_link)
  moreInfo=moreInfo_page %>% html_nodes(".product-properties")%>%html_text() %>% paste(collapse ="," )
  return(moreInfo)
}

car_data=data.frame()   #Create empty data frame

for (i in seq(1:2)){
  link=paste("https://turbo.az/autos?page=",i,sep="")
  page=read_html(link)

  car_name=page %>% html_nodes("div.products-i__name.products-i__bottom-text") %>% html_text()
  car_links=page %>% html_nodes("a.products-i__link") %>% html_attr("href") %>% paste("https://turbo.az",.,sep="")
  car_info=page %>% html_nodes("div.products-i__attributes.products-i__bottom-text") %>%html_text()
  car_price=page%>%html_nodes("div.product-price")%>%html_text()
  ad_time=page %>% html_nodes("div.products-i__datetime") %>% html_text()
  
  car_moreInfo=sapply(car_links, FUN=get_moreInfo_func, USE.NAMES = FALSE)
  
  car_data=rbind(car_data, data.frame(car_name, car_info,car_price, ad_time, car_moreInfo, car_links, stringsAsFactors = FALSE))

  print(paste("Page ",i," scrapped!"))  
}
## [1] "Page  1  scrapped!"
## [1] "Page  2  scrapped!"
#write.csv(car_data,"car_data.csv")
head(data.frame(car_data$car_name,  car_data$car_price, car_data$car_info)) #Show some columns
##   car_data.car_name car_data.car_price       car_data.car_info
## 1  Hyundai Santa Fe         33 500 AZN  2014, 2.4 L, 80 000 km
## 2   LADA (VAZ) 2110          6 800 AZN 2004, 1.5 L, 404 000 km
## 3  Bentley Bentayga          199 900 $  2017, 6.0 L, 46 820 km
## 4     Jeep Wrangler           23 500 $ 2010, 3.8 L, 155 454 km
## 5           BMW 528           24 000 $ 2014, 2.0 L, 198 000 km
## 6      Ford Transit         22 000 AZN 2007, 2.4 L, 212 000 km