R语言网页爬虫

library(rvest)
url0 <- 'http://news.sina.com.cn/china/'
web <- read_html(url0)

News <- web %>% html_nodes('div.news-item h2 a')

#---获得新闻标题---
Title <- News%>%html_text()
#---获得新闻时间---
Time <- web %>% html_nodes('div.time') %>% html_text()
#---获得链接---
link <- News %>% html_attrs()
link1 <- c(1:length(link))
for(i in 1:length(link))
{
  link1[i] <- link[[i]][1]
}
#---保存为csv文件---
#组合成数据框
dat <- data.frame(Title,Time,link1)
write.csv(dat,file = 'news.csv',row.names = FALSE)


发布了72 篇原创文章 · 获赞 37 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/xspyzm/article/details/75220229
今日推荐