温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python爬虫时怎么使用R连续抓取多个页面

发布时间:2021-08-14 15:53:07 来源:亿速云 阅读:700 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关python爬虫时怎么使用R连续抓取多个页面,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

当抓取多页的html数据,但容易被困在通用方法部分的功能上,而导致无法实现连续抓取多个页面。这个时候可以仔细观察代码当转到网页收集信息,将其添加到数据框,然后移至下一页就可以解决了。

示例:

多网页抓取时会出现以下问题。

#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

解决方案:

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-upnames(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)

以上就是连续抓取多个页面的使用方法,营销推广、爬虫数据采集、广告补量等ip问题,可以尝试下太阳http代理,助力解决多行业ip问题,免费送10000ip试用。

关于“python爬虫时怎么使用R连续抓取多个页面”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI