我想使用 r 来抓取我的网站。 "li: nth-child(n)"这部分希望n增加1。
#cMain > div.section_bestseller > div.wrap_bestseller_rest > ul > li:nth-child(1) > dl > dt > a > strong
#cMain > div.section_bestseller > div.wrap_bestseller_rest > ul > li:nth-child(2) > dl > dt > a > strong
#li:nth-child(3),li:nth-child(4) ~ li:nth-child(10)
所以我想一共提取1到10个。我该怎么办?
library(rvest)
library(httr)
all.titles <- c()
for (page in 1:10){
url='http://book.daum.net/bestseller/list.do?categoryID=SP1KOR00000&ymd=2017082&cpId=KY&pageNo='
url_page <- paste0(url,page)
reading_html <- read_html(url_page)
text_nodes <- reading_html %>% html_node('div.section_bestseller') %>% html_nodes('div.wrap_bestseller_rest') %>% html_node('ul') %>% html_node('li:nth-child(1)') %>% html_node('dl')%>% html_node('dt')%>% html_node('a')
title <- html_text(text_nodes)
all.titles<-c(all.titles, title)
print(page)
}
result<-data.frame(all.titles)
最佳答案
您需要一个 XML 解析器来从站点 (scraping) 获取信息:
out <- NULL
for(z in 1:10){
url='http://book.daum.net/bestseller/list.do?categoryID=SP1KOR00000&ymd=2017082&cpId=KY&pageNo='
hh <- htmlParse(paste0(url,z))
a <- xpathSApply(hh, "//div[@class='wrap_bestseller_rest']/*/*/*/dt/a/strong", xmlValue)
out <- cbind(out, a)
}
关于css - 网络抓取(抓取)时, "li: nth-child (n)"如何将数字 n 增加 +1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082686/