shubham tiwari
shubham tiwari

Reputation: 59

How to scrape same type of data from multiple link in R

I have link in a column in dataframe and wanted to extract same type of data from different link all in once like this

page <- read_html("https://www.airbnb.co.in/users/show/129534814")
page %>% html_nodes("._a0kct9 ._14i3z6h") %>% html_text()

Upvotes: 1

Views: 52

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174546

If your links are in a data frame like this:

df <- data.frame(links = c( "https://www.airbnb.co.in/users/show/446820235",
                            "https://www.airbnb.co.in/users/show/221530395",
                            "https://www.airbnb.co.in/users/show/74933177",
                            "https://www.airbnb.co.in/users/show/213865220",
                            "https://www.airbnb.co.in/users/show/362873365",
                            "https://www.airbnb.co.in/users/show/167648591",
                            "https://www.airbnb.co.in/users/show/143273640"))

Then you can scrape the text and store it in your data frame like this:

library(rvest)

df$greeting <- sapply(df$links, function(url) {
   read_html(url) %>% html_nodes("._a0kct9 ._14i3z6h") %>% html_text()
}, USE.NAMES = FALSE)

df
#>                                           links                     greeting
#> 1 https://www.airbnb.co.in/users/show/446820235    Hi, I’m LuxurybookingsFZE
#> 2 https://www.airbnb.co.in/users/show/221530395           Hi, I’m Blueground
#> 3  https://www.airbnb.co.in/users/show/74933177 Hi, I’m Deluxe Holiday Homes
#> 4 https://www.airbnb.co.in/users/show/213865220                 Hi, I’m Andy
#> 5 https://www.airbnb.co.in/users/show/362873365             Hi, I’m Key View
#> 6 https://www.airbnb.co.in/users/show/167648591              Hi, I’m Gregory
#> 7 https://www.airbnb.co.in/users/show/143273640            Hi, I’m AlNisreen

Upvotes: 2

Related Questions