2016-09-28 53 views
1

如何删除70页的html数据?我正在看这question,但我坚持一般方法部分的功能。Web使用R系列废弃多个系列页面

#attempt 

library(purrr) 

url_base <-"https://secure.capitalbikeshare.com/profile/trips/QNURCMF2Q6" 

map_df(1:70, function(i) { 

cat(".") 

pg <- read_html(sprintf(url_base, i)) 

data.frame(startd=html_text(html_nodes(pg, ".ed-table__col_trip-start-date")), 
endd=html_text(html_nodes(pg,".ed-table__col_trip-end-date")), 
duration=html_text(html_nodes(pg, ".ed-table__col_trip-duration")) 
) 
}) -> table 



#attempt 2 (with just one data column) 

url_base <-"https://secure.capitalbikeshare.com/profile/trips/QNURCMF2Q6" 


map_df(1:70, function(i) { 

page %>% html_nodes(".ed-table__item_odd") %>% html_text() 

}) -> table 
+0

你的网址应该有一个参数,代表当前页码,然后你应该用'url_base'粘贴它来生成实际的网址。现在看来你正在尝试访问70次相同的URL –

回答

0

@ jso1226,我不知道发生了什么事情在回答你的参考,所以我提供了一个例子非常相似的任务,你想要做什么。

这是:转到网页收集信息,添加一个数据框,然后移动到下一页。

我用创建来跟踪我的答案,在这里张贴到计算器验证码:

login<-"https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f" 

library(rvest) 
pgsession<-html_session(login) 
pgform<-html_form(pgsession)[[2]] 
filled_form<-set_values(pgform, email="*****", password="*****") 
submit_form(pgsession, filled_form) 

#pre allocate the final results dataframe. 
results<-data.frame() 

for (i in 1:5) 
{ 
    url<-"http://stackoverflow.com/users/**********?tab=answers&sort=activity&page=" 
    url<-paste0(url, i) 
    page<-jump_to(pgsession, url) 

    #collect question votes and question title 
    summary<-html_nodes(page, "div .answer-summary") 
    question<-matrix(html_text(html_nodes(summary, "div"), trim=TRUE), ncol=2, byrow = TRUE) 

    #find date answered, hyperlink and whether it was accepted 
    dateans<-html_node(summary, "span") %>% html_attr("title") 
    hyperlink<-html_node(summary, "div a") %>% html_attr("href") 
    accepted<-html_node(summary, "div") %>% html_attr("class") 

    #create temp results then bind to final results 
    rtemp<-cbind(question, dateans, accepted, hyperlink) 
    results<-rbind(results, rtemp) 
} 

#Dataframe Clean-up 
names(results)<-c("Votes", "Answer", "Date", "Accepted", "HyperLink") 
results$Votes<-as.integer(as.character(results$Votes)) 
results$Accepted<-ifelse(results$Accepted=="answer-votes default", 0, 1) 

在这种情况下,循环仅限于5页,这需要改变,以适应您的应用程序。我用******替换了用户特定的值,希望这会为您提供一些指导问题。