我试图使用以下循环从IMDB中刮取日期、标题和评论:
library(rvest)
library(dplyr)
library(stringr)
library(tidyverse)
ID <- 4633694
data <- lapply(paste0('http://www.imdb.com/title/tt', ID, '/reviews?filter=prolific', 1:20),
function(url){
url %>% read_html() %>%
html_nodes(".review-date,.rating-other-user-rating,.title,.show-more__control") %>%
html_text() %>%
gsub('[\r\n\t]', '', .)
})它以下列格式提供20页的审阅数据,重复相同的模式:
col1
1 10/10
2 If this was..
3 14 December 2018
4 I have to say, and no...
5
6
7 10/10
8 Stan Lee Is Smiling Right Now...
9 17 December 2018
10 A movie worthy of...
11
12
13 10/10
14 the most visually stunning film I've ever seen...
15 20 December 2018
16 There's hardly anything...
17.
18.我想知道是否有一种方法可以将每4行转换为单个列,以便每个属性在适当的列中对齐,如下所示:
Date Rating Title Review
1. 14 December 2018 10/10 If this was.. I have to...
2. 17 December 2018 10/10 Stan Lee Is... A movie worthy...
3. 20 December 2018 10/10 the most visually.. There's hardly anything...发布于 2019-02-17 08:08:18
text_data = gsub('\\b(\\d+/\\d+)\\b','\n\\1',paste(grep('\\w',x$col1,value = TRUE),collapse = ':'))
read.csv(text=text_data,h=F,sep=":",strip.white = T,fill=T,stringsAsFactors = F)
V1 V2 V3 V4 V5
1 10/10 If this was.. 14 December 2018 I have to say, and no... NA
2 10/10 Stan Lee Is Smiling Right Now... 17 December 2018 A movie worthy of... NA
3 10/10 the most visually stunning film I've ever seen... 20 December 2018 There's hardly anything... NA发布于 2019-02-17 05:59:45
这里有一个方法。
数据:
x <- read.csv2(header=TRUE, stringsAsFactors=FALSE, text="
col1
10/10
If this was..
14 December 2018
I have to say, and no...
10/10
Stan Lee Is Smiling Right Now...
17 December 2018
A movie worthy of...
10/10
the most visually stunning film I've ever seen...
20 December 2018
There's hardly anything...
.
.")首先,我们“找到”每一行顶部,在这种情况下,它看起来像一个日期。请注意,您可能希望/需要微调这个正则表达式,将假阳性和假阴性最小化。
ind <- grep("^[0-9]+/[0-9]+", x$col1)
x$col1[ind]
# [1] "10/10" "10/10" "10/10"底线是将每个块的第一行的索引放入ind中。
从这里开始,让我们将每个块提取到下一个块开始的位置(减去1),直到框架列的末尾:
y <- Map(function(a,b) x$col[a:b], ind, c(ind[-1], nrow(x)))
str(y)
# List of 3
# $ : chr [1:5] "10/10" "If this was.." "14 December 2018" "I have to say, and no..." ...
# $ : chr [1:5] "10/10" "Stan Lee Is Smiling Right Now..." "17 December 2018" "A movie worthy of..." ...
# $ : chr [1:6] "10/10" "the most visually stunning film I've ever seen..." "20 December 2018" "There's hardly anything... " ...我们可以尝试跳到前面(下面的do.call ),但是它会遇到问题,因为我们的向量有不同的大小。通过将它们的长度设置为最长向量的长度,我们可以很容易地解决这个问题。这里有个技巧可以做到:
z <- lapply(y, `length<-`, max(lengths(y)))
str(z)
# List of 3
# $ : chr [1:6] "10/10" "If this was.." "14 December 2018" "I have to say, and no..." ...
# $ : chr [1:6] "10/10" "Stan Lee Is Smiling Right Now..." "17 December 2018" "A movie worthy of..." ...
# $ : chr [1:6] "10/10" "the most visually stunning film I've ever seen..." "20 December 2018" "There's hardly anything... " ...最后一步:
setNames(do.call("rbind.data.frame", c(z, stringsAsFactors=FALSE)),
letters[seq_len(length(z[[1]]))])
# a b c
# 1 10/10 If this was.. 14 December 2018
# 2 10/10 Stan Lee Is Smiling Right Now... 17 December 2018
# 3 10/10 the most visually stunning film I've ever seen... 20 December 2018
# d e f
# 1 I have to say, and no... 10/10 <NA>
# 2 A movie worthy of... 10/10 <NA>
# 3 There's hardly anything... . .https://stackoverflow.com/questions/54730453
复制相似问题