加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

R语言—豆瓣搜索电影

发布时间:2020-12-16 05:08:10 所属栏目:百科 来源:网络整理
导读:豆瓣搜索想要的电影名并返回电影评分,走起 library(RCurl)library(XML)movieScore - function(x) { stopifnot(is.character(x)) # 提交搜索豆瓣表单 search - getForm("http://movie.douban.com/subject_search",search_text = x) searchweb - htmlParse(se
豆瓣搜索想要的电影名并返回电影评分,走起
library(RCurl)
library(XML)
movieScore <- function(x) {
    stopifnot(is.character(x))
    # 提交搜索豆瓣表单
    search <- getForm("http://movie.douban.com/subject_search",search_text = x)
    searchweb <- htmlParse(search)
    # 解析搜索结果页面
    resnodes <- getNodeSet(searchweb,"//div[@id='wrapper']//table[1]//a")
    if (is.null(resnodes)) 
        return(NULL) else resurl <- xmlGetAttr(resnodes[[1]],name = "href")
    # 得到影片页面后第二次解析
    resweb <- getURL(resurl,.encoding = "UTF-8")
    content <- htmlParse(resweb,encoding = "UTF-8")
    resnodes <- getNodeSet(content,"//div[@id='interest_sectl']//p[@class='rating_self clearfix']//strong")
    namenodes <- getNodeSet(content,"//div[@id='content']//h1//span")
    # 得到影片评分
    score <- xmlValue(resnodes[[1]])
    name <- xmlValue(namenodes[[1]])
    return(list(name = name,score = score))
}
看看天机这部大烂片多少分。
movieScore("天机")
## $name
## [1] "天机·富春山居图"
## 
## $score
## [1] "2.9"
抓网页比较慢,豆瓣为人民群众着想提供了API,我们也可以使用API来调取分数,函数也比较简单。
library(RCurl)
library(XML)
library(RJSONIO)
movieScoreapi <- function(x) {
    api <- "https://api.douban.com/v2/movie/search?q={"
    url <- paste(api,x,"}",sep = "")
    res <- getURL(url)
    reslist <- fromJSON(res)
    name <- reslist$subjects[[1]]$title
    score <- reslist$subjects[[1]]$rating$average
    return(list(name = name,score = score))
}
movieScoreapi("僵尸世界大战")
## $name
## [1] "僵尸世界大战"
## 
## $score
## [1] 7.5
有了这个查分函数,我们可以在R中批量查阅电影评分了。但是豆瓣对于频繁的访问会有限制,对于没有认证的API使用是每分钟10次,超过就会暂时封IP。对于网页抓取,肖楠在第六次R会议上有个很棒的演讲,有兴趣的同学可以去统计之都看看。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读