Q:

scraping Q&A works fine except when there's more than one page of answers for one post

the following code scrapes all questions and answers with their authors and dates, but I cannot figure out how to scarpe also answers that take more than one page, for example for the second question here
https://www.healthboards.com/boards/aspergers-syndrome/index2.html
Asperger's and talking to yourself
Answers are in 2 pages: 15 in the firt page and 3 in the second, I'm getting the answers in the first page only
library(rvest)
library(dplyr)
library(stringr)
library(purrr)
library(tidyr)
library(RCurl)
library(xlsx)
#install.packages("xlsx")
# Scrape thread titles, thread links, authors and number of views

url <- "https://www.healthboards.com/boards/aspergers-syndrome/index2.html"

h <- read_html(url)

threads <- h %>%
  html_nodes("#threadslist .alt1 div > a") %>%
  html_text()
threads

thread_links <- h %>%
  html_nodes("#threadslist .alt1 div > a") %>%
  html_attr(name = "href")
thread_links

thread_starters <- h %>%
  html_nodes("#threadslist .alt1 div.smallfont") %>%
  html_text() %>%
  str_replace_all(pattern = "\t|\r|\n", replacement = "")

thread_starters

views <- h %>%
  html_nodes(".alt2:nth-child(6)") %>%
  html_text() %>%
  str_replace_all(pattern = ",", replacement = "") %>%
  as.numeric()

# Custom functions to scrape author IDs and posts

scrape_posts <- function(link){
  read_html(link) %>%
    html_nodes(css = ".smallfont~ hr+ div") %>%
    html_text() %>%
    str_replace_all(pattern = "\t|\r|\n", replacement = "") %>%
    str_trim()
}

scrape_dates <- function(link){
  read_html(link) %>%
    html_nodes(css = "table[id^='post'] td.thead:first-child") %>%
    html_text() %>%
    str_replace_all(pattern = "\t|\r|\n", replacement = "") %>%
    str_trim()
}

scrape_author_ids <- function(link){
  h <- read_html(link) %>%
    html_nodes("div") 

  id_index <- h %>%
    html_attr("id") %>%
    str_which(pattern = "postmenu")

  h %>%
    `[`(id_index) %>%
    html_text() %>%
    str_replace_all(pattern = "\t|\r|\n", replacement = "") %>%
    str_trim()
}

htmls <- map(thread_links, getURL)

# Create master dataset

master_data <- 
  tibble(threads, thread_starters,thread_links) %>%
  mutate(
    post_author_id = map(htmls, scrape_author_ids),
    post = map(htmls, scrape_posts),
    fec=map(htmls, scrape_dates)
  ) %>%
  select(threads: post_author_id, post, thread_links,fec) %>%
  unnest()

master_data$thread_starters
threads
post
titles<-master_data$threads
therad_starters<-master_data$thread_starters
#views<-master_data$views

post_author<-master_data$post_author_id
post<-master_data$post
fech<-master_data$fec
employ.data <- data.frame(titles, therad_starters, post_author, post,fech)

write.xlsx(employ.data, "C:/2.xlsx")

Can't figure out how to include also the second page..

A:

Taking a quick look at your code and the website, there is a td under class vbmenu_control which holds the number of pages (in your case, page 2 of 2). You could use some simple regex such as 
a = "page 2 of 2"
b = as.numeric(gsub("page 2 of ","",a))
> b
[1] 2

And add a conditional if b>1. If this is TRUE, you can loop-scrape through the link ...-talking-yourself-i.html with i being the values from the sequence 1 to the number of pages.

