R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Step 1

library('selectr')
library('xml2')
library('rvest')
library('stringr')
scrappedurl<-'https://www.amazon.in/OnePlus-Mirror-Black-64GB-Memory/dp/B0756Z43QS?tag=googinhydr18418-21&tag=googinkenshoo-21&ascsubtag=aee9a916-6acd-4409-92ca-3bdbeb549f80'
webpage<-read_html('https://www.amazon.in/OnePlus-Mirror-Black-64GB-Memory/dp/B0756Z43QS?tag=googinhydr18418-21&tag=googinkenshoo-21&ascsubtag=aee9a916-6acd-4409-92ca-3bdbeb549f80')
title_html<-html_nodes(webpage,'h1#title')
title<-html_text(title_html)
title<-str_replace_all(title,"[\r\n]","")
head(title)
## [1] "OnePlus 6 (Mirror Black, 6GB RAM, 64GB Storage)"

Step 2

price_html<-html_nodes(webpage,'span#priceblock_ourprice')
price<-html_text(price_html)
price<-str_replace_all(price,"[\r\n]","")
price<-str_trim(price)
head(price)
## character(0)

Step 3

desc_html<-html_nodes(webpage,'div#productDescription')
desc<-html_text(desc_html)
description<-str_replace_all(desc,"[\r\n]","")
description<-str_trim(description)
head(description)
## character(0)

Step 4

rating_html<-html_nodes(webpage,'span#acrPopover')
rating<-html_text(rating_html)
rating<-str_replace_all(rating,"[\r\n]","")
rate<-str_trim(rating)
head(rate)
## [1] "4.6 out of 5 stars" "4.6 out of 5 stars"

Step 5

size_html<-html_nodes(webpage, 'div#variation_size_name')
size_html<-html_nodes(size_html,'span.selection')
size<-html_text(size_html)
size<-str_replace_all(size,"[\r\n]","")
size<-str_trim(size)
head(size)
## [1] "64 GB"

Step 6

color_html<-html_nodes(webpage,'div#variation_color_name')
color_html<-html_nodes(color_html,'span.selection')
color<-html_text(color_html)
color<-str_replace_all(color,"[\r\n]","")
color<-str_trim(color)
head(color)
## [1] "black"

Step 7

product_data<-data.frame(Title=title,Rating=rate,Size=size,Color=color)
dataframe<-str(product_data)
## 'data.frame':    2 obs. of  4 variables:
##  $ Title : chr  "OnePlus 6 (Mirror Black, 6GB RAM, 64GB Storage)" "OnePlus 6 (Mirror Black, 6GB RAM, 64GB Storage)"
##  $ Rating: chr  "4.6 out of 5 stars" "4.6 out of 5 stars"
##  $ Size  : chr  "64 GB" "64 GB"
##  $ Color : chr  "black" "black"

Step 8

library(jsonlite)
json_data<-toJSON(product_data)
finaldata<-cat(json_data)
## [{"Title":"OnePlus 6 (Mirror Black, 6GB RAM, 64GB Storage)","Rating":"4.6 out of 5 stars","Size":"64 GB","Color":"black"},{"Title":"OnePlus 6 (Mirror Black, 6GB RAM, 64GB Storage)","Rating":"4.6 out of 5 stars","Size":"64 GB","Color":"black"}]