小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

爬取豆瓣電影top 250

 萌小芊 2018-01-19

      晚上寫的論文沒有保存,要被自己蠢哭~然后就開始爬豆瓣電影250,總共有250部電影,10頁。但在第5頁和第10頁時爬取的電影的簡要介紹有缺失,這會造成該變量與其他變量長度不一,不能弄在同一個data.frame里面。然后弄成list,又不能實現(xiàn)rbind函數(shù)。所以我就用了比較笨的辦法,把第5頁和第10頁的函數(shù)單獨編輯~希望可以有大神寫出更棒的代碼和我交流~


library(magrittr)

library(rvest)

library(xml2)

library(stringr)

site1<-'https://movie.douban.com/top250?start='

site2<-'&filter='

movie<-data.frame()

for(i in 1:4){

  fun<-function(i){

  site<-paste(site1,25*(9-1),site2,sep='')

  web<-read_html(site)

  name<-web%>%html_nodes('.title:nth-child(1)')%>%html_text()%>%str_trim()

  introduction<-web%>%html_nodes('.inq')%>%html_text()

  remark<-web%>%html_nodes('.rating_num')%>%html_text()%>%as.numeric()

  number1<-web%>%html_nodes('.rating_num~span')%>%html_text()%>%str_trim()

  no<-seq(2,50,2)

  number<-number1[no]

  movie<-data.frame(name,introduction,remark,number)

  }

movie<-rbind(movie,fun(i))

}

######在第5頁時introduction變量只有24個,核對之后發(fā)現(xiàn)是第5頁第四部《摔跤吧 爸爸》沒有介紹。因為能力有限,只能對第五頁進行單獨爬。

introduction<-NULL

fun<-function(i){

  site<-paste(site1,25*(i-1),site2,sep='')

  web<-read_html(site)

  name<-web%>%html_nodes('.title:nth-child(1)')%>%html_text()%>%str_trim()

  introduction1<-web%>%html_nodes('.inq')%>%html_text()

  introduction<-c(introduction1[1:3],'無介紹',introduction1[4:24])

  remark<-web%>%html_nodes('.rating_num')%>%html_text()%>%as.numeric()

  number1<-web%>%html_nodes('.rating_num~span')%>%html_text()%>%str_trim()

  no<-seq(2,50,2)

  number<-number1[no]

  movie<-data.frame(name,introduction,remark,number)

}

movie<-rbind(movie,fun(5))

########繼續(xù)第6-9頁

for(i in 6:9){

  fun<-function(i){

    site<-paste(site1,25*(9-1),site2,sep='')

    web<-read_html(site)

    name<-web%>%html_nodes('.title:nth-child(1)')%>%html_text()%>%str_trim()

    introduction<-web%>%html_nodes('.inq')%>%html_text()

    remark<-web%>%html_nodes('.rating_num')%>%html_text()%>%as.numeric()

    number1<-web%>%html_nodes('.rating_num~span')%>%html_text()%>%str_trim()

    no<-seq(2,50,2)

    number<-number1[no]

    movie<-data.frame(name,introduction,remark,number)

  }

  movie<-rbind(movie,fun(i))

}

######在第10頁時introduction變量只有24個,核對之后發(fā)現(xiàn)是第10頁第20部《你的名字》沒有介紹。還是按照第5頁的寫法。

introduction<-NULL

fun<-function(i){

  site<-paste(site1,25*(10-1),site2,sep='')

  web<-read_html(site)

  name<-web%>%html_nodes('.title:nth-child(1)')%>%html_text()%>%str_trim()

  introduction1<-web%>%html_nodes('.inq')%>%html_text()

  introduction<-c(introduction1[1:19],'無介紹',introduction1[20:24])

  remark<-web%>%html_nodes('.rating_num')%>%html_text()%>%as.numeric()

  number1<-web%>%html_nodes('.rating_num~span')%>%html_text()%>%str_trim()

  no<-seq(2,50,2)

  number<-number1[no]

  movie<-data.frame(name,introduction,remark,number)

}

movie<-rbind(movie,fun(10))

write.csv(movie,'C:\\Users\\Administrator\\Desktop\\movie.csv')


一入爬蟲深似海,從此復(fù)制是路人~

有興趣的可以試一下,不用粘貼復(fù)制就可以得到豆瓣top250的電影了

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多