How do I gather all the urls in this table using rvest?

Viewed 45

I am trying to get all the links in the first column of the table here

I can only get the first link/row.

library(rvest)
        url <- "https://di.hkex.com.hk/di/NSSrchPersonList.aspx?sa1=pl&scsd=01/01/2018&sced=31/12/2018&pn=wing&src=MAIN&lang=EN"   

        l <-    wahis.session %>%
          html_nodes(xpath = '//*[@id="grdPaging"]') %>%
          map_chr(~html_attr(html_node(., "a"), "href"))

        l <- as.data.frame(l)
2 Answers

rvest supports nth-of-type pseudo class css selector so you can use the following for child a tags of first column tds of table with specified id

library(rvest)
url <- "https://di.hkex.com.hk/di/NSSrchPersonList.aspx?sa1=pl&scsd=01/01/2018&sced=31/12/2018&pn=wing&src=MAIN&lang=EN"   
links <- url %>%
  read_html() %>%
  html_nodes("#grdPaging td:nth-of-type(1) a") %>%
  html_attr("href")

One option is to gather all the anchor nodes of the table and get all the href attribute.

library(rvest)
url <- "https://di.hkex.com.hk/di/NSSrchPersonList.aspx?sa1=pl&scsd=01/01/2018&sced=31/12/2018&pn=wing&src=MAIN&lang=EN"   

url %>%
   read_html() %>%
   html_nodes(xpath = '//*[@id="grdPaging"]') %>%
   html_nodes("a") %>%
   html_attr("href")

# [1] "NSNoticePersonList.aspx?sa2=np&scpid1=35225&scpid3=0&scpid2=67774&sa1=pl&scsd=01%2f01%2f2018&sced=31%2f12%2f2018&pn=wing&src=MAIN&lang=EN&" 
# [2] "NSNoticePersonList.aspx?sa2=np&scpid1=30212&scpid3=0&scpid2=4677&sa1=pl&scsd=01%2f01%2f2018&sced=31%2f12%2f2018&pn=wing&src=MAIN&lang=EN&"  
# [3] "NSNoticePersonList.aspx?sa2=np&scpid1=32746&scpid3=0&scpid2=8439&sa1=pl&scsd=01%2f01%2f2018&sced=31%2f12%2f2018&pn=wing&src=MAIN&lang=EN&"  
#.....
Related