为什么在R中创建数据帧时会复制数据?

为什么在R中创建数据帧时会复制数据?,r,dataframe,R,Dataframe,我是R的新手,现在正在尝试使用R进行web抓取。我得到了我需要的所有信息,并希望将它们组合成一个数据框架。但当我整理这些信息时,我发现我的信息都是重复的。以下是我的代码和输出: library(xml2) library(rvest) library(stringr) url<-'https://www.amazon.in/OnePlus-Midnight-Black-256GB-Storage/dp/B077PWBC6V/ref=dp_prsubs_1?pd_rd_i=B077PWBC6

我是R的新手,现在正在尝试使用R进行web抓取。我得到了我需要的所有信息,并希望将它们组合成一个数据框架。但当我整理这些信息时,我发现我的信息都是重复的。以下是我的代码和输出:

library(xml2)
library(rvest)
library(stringr)
url<-'https://www.amazon.in/OnePlus-Midnight-Black-256GB-Storage/dp/B077PWBC6V/ref=dp_prsubs_1?pd_rd_i=B077PWBC6V&psc=1'
webpage<- read_html(url)
title_html<- html_nodes(webpage,'h1#title')
title<-html_text(title_html)
head(title)
#> [1] "\n\n\n\n\n\n\n\n\nOnePlus 6 (Midnight Black, 8GB RAM, 256GB Storage)\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
str_replace_all(title,"[\r\n]","")
#> [1] "OnePlus 6 (Midnight Black, 8GB RAM, 256GB Storage)"
price_html<-html_nodes(webpage,'span#priceblock_ourprice')
price<-html_text(price_html)
str_replace_all(price,"[\r\n]","")
#> [1] "\u20b9 43,999.00"
desc_html <- html_nodes(webpage, 'div#productDescription')
desc <- html_text(desc_html)
desc<-str_replace_all(desc,"[\r\n\t]","")
desc<-str_trim(desc)
head(desc)
#> [1] "Size name:256GB | Colour:blackThe OnePlus 6 comes with a 19:9 Full Optic AMOLED display, 20+16 MP dual primary camera, 6/8 GB of RAM; up to 256 GB memory, Snapdragon 845 processor and much more"
rate_html<-html_nodes(webpage,'span#acrPopover')
rate<-html_text(rate_html)
rate<- str_replace_all(rate,"[\r\n]","")
rate<- str_trim(rate)
head(rate)
#> [1] "4.6 out of 5 stars" "4.6 out of 5 stars"
size_html<- html_nodes(webpage,'div#variation_size_name')
size_html<-html_nodes(size_html, 'span.selection')
size<- html_text(size_html)
size<-str_trim(size)
head(size)
#> [1] "256GB"
color_html <- html_nodes(webpage, 'div#variation_color_name')
color_html <- html_nodes(color_html, 'span.selection')
color <- html_text(color_html)
color <- str_trim(color)
head(color)
#> [1] "black"
product_data <- data.frame(Title = title, Price = price,Description = desc, Rating = rate, Size = size, Color = color)
str(product_data)
#> 'data.frame':    2 obs. of  6 variables:
#>  $ Title      : chr  "\n\n\n\n\n\n\n\n\nOnePlus 6 (Midnight Black, 8GB RAM, 256GB Storage)\n\n\n\n\n\n\n\n\n\n\n\n\n\n" "\n\n\n\n\n\n\n\n\nOnePlus 6 (Midnight Black, 8GB RAM, 256GB Storage)\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
#>  $ Price      : chr  "<U+20B9> 43,999.00" "<U+20B9> 43,999.00"
#>  $ Description: chr  "Size name:256GB | Colour:blackThe OnePlus 6 comes with a 19:9 Full Optic AMOLED display, 20+16 MP dual primary "| __truncated__ "Size name:256GB | Colour:blackThe OnePlus 6 comes with a 19:9 Full Optic AMOLED display, 20+16 MP dual primary "| __truncated__
#>  $ Rating     : chr  "4.6 out of 5 stars" "4.6 out of 5 stars"
#>  $ Size       : chr  "256GB" "256GB"
#>  $ Color      : chr  "black" "black"
库(xml2)
图书馆(rvest)
图书馆(stringr)

url之所以有重复项,是因为费率信息有重复的行。我为您创建了一个helper函数,这样您就不必重复自己的操作(DRY原则)。在这里,我插入了一个对
unique()
的调用,以确保不会得到重复的代码。正如Gavin在评论中指出的,您没有重新分配标题,这就是为什么
\n
仍然保留在标题中的原因。让我知道,如果下面的帮助

library(xml2)
library(rvest)
library(stringr)
library(dplyr)
url <- paste(
  'https://www.amazon.in/OnePlus-Midnight-Black-256GB-Storage/dp',
  '/B077PWBC6V/ref=dp_prsubs_1?pd_rd_i=B077PWBC6V&psc=1',
  sep = ''
)
webpage <- read_html(url)

parseInfo <- function(webpage, node) {
  info <- webpage %>% 
    html_nodes(node) %>% 
    html_text() %>% 
    str_replace_all('[\r\n\t]', '') %>% 
    str_trim() %>% 
    unique()
  return(info)
}

title <- parseInfo(webpage, 'h1#title')
price <- parseInfo(webpage, 'span#priceblock_ourprice')
desc <- parseInfo(webpage, 'div#productDescription')
rate <- parseInfo(webpage, 'span#acrPopover')
size <- parseInfo(webpage, 'div#variation_size_name')
color <- parseInfo(webpage, 'div#variation_color_name')

product_data <- data.frame(
  Title = title,
  Price = price, 
  Description = desc,
  Rating = rate, 
  Size = size, 
  Color = color
)
库(xml2)
图书馆(rvest)
图书馆(stringr)
图书馆(dplyr)
url%
str_trim()%%>%
唯一的()
返回(信息)
}

title不确定您的其他问题-但是剩余的换行符
\n
是因为您没有用
str\u replace\u all
调用覆盖title变量,如第行:
str\u replace\u all(title,[\r\n],“”)
,即我看到的应该是
title!非常感谢。通过更改
rate\u htmly进行重复数据消除是有帮助的!非常感谢。但我不明白“费率信息有重复行”是什么意思,你能解释一下吗?没问题。作为一个实验,尝试将helper函数更改为不调用
unique()
。如果你运行这个速率并检查它,你会看到速率是一个向量,向量是
“5颗星中的4.6颗星”,“5颗星中的4.6颗星”
。这就是为什么您的案例中的整个数据帧有两个相同的行的原因。调用
unique()
缓解了这一问题。如果答案有帮助,请毫不犹豫地接受它。我明白了,但为什么会这样?问题是来自网页还是代码?因此,由于速率的原因,其他信息也会重复?是的-因为数据帧的一个输入的行数是其他输入的两倍,所以其他输入也会重复。我相信哈德利·维克汉姆在年写过这件事。我现在能理解了。谢谢你清楚的解释!