豆瓣排名前25电影及评价爬取
url <-'http://movie.douban.com/top250?format=text'
# 获取网页原代码,以行的形式存放在web 变量中
web <- readLines(url,encoding="UTF-8")
# 找到包含电影名称的行
name <- str_extract_all(string = web, pattern = '<span class="title">.+</span>')
movie.names_line <- unlist(name)
# 用正则表达式来提取电影名
movie.names <- str_extract(string = movie.names_line, pattern = ">[^&].+<") %>%
str_replace_all(string = ., pattern = ">|<",replacement = "")
movie.names <- na.omit(movie.names)
# 获取评价人数
Rating <- str_extract_all(string = web,pattern = '<span>[:digit:]+人评价</span>')
Rating.num_line <- unlist(Rating)
Rating.num <- str_extract(string = Rating.num_line, pattern = "[:digit:]+") %>% as.num