css - 网络抓取(抓取)时, "li: nth-child (n)"如何将数字 n 增加 +1?

标签 css r web-scraping web-crawler

我想使用 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/

相关文章:

javascript - 鼠标悬停时显示 div 无法正常工作

python-3.x - os.path.isfile 出现 "file path not defined"错误?

jquery - 单击按钮时的 Animate.css Fliptext 效果不佳

css - 悬停时突出显示下拉列表中的元素,并确保突出显示的颜色延伸到下拉框的右边缘

PHP preg_match_all 正则表达式从 css 获取像素值( "px")

r - 更改 R 中图形的字体

java - 如何包含 R 包的 java 依赖项

r - 如何从 R 查询 MongoDB?

Python,将所有链接、标题和正文文本附加到一个数组或 json 文件中

python - 使用 BeautifulSoup 进行 SRE 匹配时无法访问元素