2
我创建从下面的新闻源RSS http://indianexpress.com/section/india/feed/无法凑新闻网站
我从这个XML
- 标题
- 标题URL
- 出版日期阅读下面的数据的数据集
我现在使用标题url来获取de (摘要,在主标题下方) - 通过点击每个网址并获取数据
但是,我正面临着向量长度(197)与其他(200)的描述不匹配。 因为这个我无法创建我的数据帧
有人能帮助我如何能有效地刮去数据
下面的代码是可重复的
library("httr")
library("RCurl")
library("jsonlite")
library("lubridate")
library("rvest")
library("XML")
library("stringr")
url = "http://indianexpress.com/section/india/feed/"
newstopics = getURL(url)
newsxml = xmlParse(newstopics)
title <- xpathApply(newsxml, "//item/title", xmlValue)
title <- unlist(title)
titleurl <- xpathSApply(newsxml, '//item/link', xmlValue)
pubdate <- xpathSApply(newsxml, '//item/pubDate', xmlValue)
t1 = Sys.time()
desc <- NULL
for (i in 1:length(titleurl)){
page = read_html(titleurl[i])
temp = html_text(html_nodes(page,'.synopsis'))
desc = c(desc,temp)
}
print(difftime(Sys.time(), t1, units = 'sec'))
desc = gsub("\n",' ',desc)
newsdata = data.frame(title,titleurl,desc,pubdate)
我收到以下错误:
Error in data.frame(title, titleurl, desc, pubdate) :
arguments imply differing number of rows: 200, 197
我认为这个问题是关系到'temp'不会为'for'循环中的每个迭代返回一个值。尝试用'desc = c(desc,paste0(“”,temp))'替换'desc'行 - 尽管更优雅的错误处理是期望的。 – JasonAizkalns
我检查了titleurl在任何地方都不为空。我假设由于每个网址都是一个报纸链接,他们肯定会有一个副标题 –