【问题标题】:How to get date from time element in R?如何从R中的时间元素获取日期?
【发布时间】:2019-09-24 12:35:30
【问题描述】:

我正在尝试从website 中的时间元素获取日期,但结果为 NA。

url <- "https://www.trustpilot.com/review/www.spotify.com"

dates <- read_html(url) %>% 
  html_nodes("div.review-content-header__dates") %>% 
  html_attr("datetime")

怎么了?

提前致谢。

【问题讨论】:

  • 上面没有这样的节点名!
  • 你想要哪个日期?发布日期?您定位的节点包含:&lt;div class="review-content-header__dates"&gt; &lt;script type="application/json" data-initial-state="review-dates"&gt; {"publishedDate":"2019-09-12T08:00:12Z", "updatedDate":null,"reportedDate":null} ...
  • @Chelmy88 发布日期。

标签: r web-scraping rvest


【解决方案1】:

如果你想要的是&lt;script&gt; 节点内的字段publishedDate,你可以这样做:

library(rvest)

url <- "https://www.trustpilot.com/review/www.spotify.com"
dates <- read_html(url) %>% 
  html_nodes("div.review-content-header__dates") %>%
  html_text()

# Now do some cleaning on the obtained data
# Remove extra spaces
dates <- gsub('\\s+','',dates)
# Remove text before the date
dates <- gsub('\\{\"publishedDate\":\"','',dates)
# Remove text after the date
dates <- gsub('Z\",\".*','',dates)

dates

 > [1] "2019-09-23T15:02:07" "2019-09-21T15:24:14" "2019-09-20T15:16:35"
 > [4] "2019-09-20T13:45:35" "2019-09-19T14:48:44" "2019-09-18T02:56:34"
 > [7] "2019-09-16T00:24:32" "2019-09-13T00:04:14" "2019-09-12T19:47:27"
 > [10] "2019-09-12T12:59:54" "2019-09-12T08:00:12" "2019-09-11T13:18:01"
 > [13] "2019-09-10T08:07:54" "2019-09-05T16:16:53" "2019-09-05T14:17:42"
 > [16] "2019-09-04T19:49:28" "2019-09-04T18:33:04" "2019-09-02T18:45:53"
 > [19] "2019-08-31T20:53:44" "2019-08-30T23:24:25"

【讨论】:

    【解决方案2】:

    如果您想知道如何提取脚本标签内容并解析为 json 对象,然后提取评论数据框(包括提取发布日期的日期部分)

    library(rvest)
    library(magrittr)
    library(jsonlite)
    library(purrr)
    
    r <- jsonlite::parse_json(gsub(';|\\n+','',read_html('https://www.trustpilot.com/review/www.spotify.com') %>%
         html_node('[data-business-unit-json-ld]') %>% html_text()))
    
    df <- map_df(r[[1]]$review, function(item) {
    
      data.frame( author = item$author$name,
                  date = strsplit(item$datePublished,"T")[[1]][1] ,
                  headline = item$headline,
                  reviewBody = item$reviewBody,
                  rating = item$reviewRating$ratingValue,
                 stringsAsFactors=FALSE)
    })
    
    View(df)
    

    示例输出:

    【讨论】:

      【解决方案3】:
      url <- "https://www.trustpilot.com/review/www.spotify.com"
      dates <- read_html(url) %>% 
        html_nodes("div.review-content-header__dates") %>%
        html_text() %>% 
        stringi::stri_trim_both() #here
      
      dates <- gsub('\\{\"publishedDate\":\"','',dates)
      dates <- gsub('\\T.*','',dates) #and here
      dates
      

      您可以通过与上述@Chelmy88 略有不同的代码获取日期。

      【讨论】:

        【解决方案4】:

        这是使用sub提取日期的另一种方法

        url <- "https://www.trustpilot.com/review/www.spotify.com"
        library(rvest)
        
        url %>%
          read_html() %>%
          html_nodes("div.review-content-header__dates") %>%
          html_text() %>%
          sub('.*publishedDate.*(\\d{4}-\\d{2}-\\d{2}).*', '\\1', .)
        
        # [1] "2019-09-23" "2019-09-21" "2019-09-20" "2019-09-20" "2019-09-19" "2019-09-18" "2019-09-16"
        # [8] "2019-09-13" "2019-09-12" "2019-09-12" "2019-09-12" "2019-09-11" "2019-09-10" "2019-09-05"
        #[15] "2019-09-05" "2019-09-04" "2019-09-04" "2019-09-02" "2019-08-31" "2019-08-30"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-20
          • 2013-12-09
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 2020-06-02
          相关资源
          最近更新 更多