【问题标题】:Web-scraping has weird format of date, need to convert. "Error in charToDate(x) : character string is not in a standard unambiguous format"网页抓取有奇怪的日期格式,需要转换。 “charToDate(x) 中的错误:字符串不是标准的明确格式”
【发布时间】:2021-01-20 15:46:28
【问题描述】:

我每天都在尝试抓取这个网站。 https://www.basketball-reference.com/boxscores/?month=9&day=30&year=2020 如您所见,日期格式不正确,我不知道如何让 R 将其识别为日期。

到目前为止,这是我的代码:

url <- "https://www.basketball-reference.com/boxscores/"

timevalues <- seq(as.Date("month=10&day=2&year=2020"), as.Date("month=10&day=2&year=2020"), by = "day")
head(timevalues)

charToDate(x) 中的错误:字符串不是标准的明确格式

【问题讨论】:

    标签: r web-scraping as.date


    【解决方案1】:

    您不能像这样生成序列(或日期)。这是使用 lubridate 包的解决方案

    library(lubridate)
    
    url <- "https://www.basketball-reference.com/boxscores/"
    
    my_dates <- seq(as.Date("2020-09-25"), as.Date("2020-10-05"), by = "day")
    
    urls <- paste0(url, 
                   "?month=", month(my_dates),
                   "&day=", day(my_dates), 
                   "&year=", year(my_dates))
    
    urls
    #>  [1] "https://www.basketball-reference.com/boxscores/?month=9&day=25&year=2020"
    #>  [2] "https://www.basketball-reference.com/boxscores/?month=9&day=26&year=2020"
    #>  [3] "https://www.basketball-reference.com/boxscores/?month=9&day=27&year=2020"
    #>  [4] "https://www.basketball-reference.com/boxscores/?month=9&day=28&year=2020"
    #>  [5] "https://www.basketball-reference.com/boxscores/?month=9&day=29&year=2020"
    #>  [6] "https://www.basketball-reference.com/boxscores/?month=9&day=30&year=2020"
    #>  [7] "https://www.basketball-reference.com/boxscores/?month=10&day=1&year=2020"
    #>  [8] "https://www.basketball-reference.com/boxscores/?month=10&day=2&year=2020"
    #>  [9] "https://www.basketball-reference.com/boxscores/?month=10&day=3&year=2020"
    #> [10] "https://www.basketball-reference.com/boxscores/?month=10&day=4&year=2020"
    #> [11] "https://www.basketball-reference.com/boxscores/?month=10&day=5&year=2020"
    

    【讨论】:

      【解决方案2】:

      我们可以使用glue来创建

      library(lubridate)
      url <- "https://www.basketball-reference.com/boxscores/"
      my_dates <- seq(as.Date("2020-09-25"), as.Date("2020-10-05"), by = "day")
      urls <- glue::glue("{url}?month={month(my_dates)}&day={day(my_dates)}",
                     "&year={year(my_dates)}")
      

      -输出

      #https://www.basketball-reference.com/boxscores/?month=9&day=25&year=2020
      #https://www.basketball-reference.com/boxscores/?month=9&day=26&year=2020
      #https://www.basketball-reference.com/boxscores/?month=9&day=27&year=2020
      #https://www.basketball-reference.com/boxscores/?month=9&day=28&year=2020
      #https://www.basketball-reference.com/boxscores/?month=9&day=29&year=2020
      #https://www.basketball-reference.com/boxscores/?month=9&day=30&year=2020
      #https://www.basketball-reference.com/boxscores/?month=10&day=1&year=2020
      #https://www.basketball-reference.com/boxscores/?month=10&day=2&year=2020
      #https://www.basketball-reference.com/boxscores/?month=10&day=3&year=2020
      #https://www.basketball-reference.com/boxscores/?month=10&day=4&year=2020
      #https://www.basketball-reference.com/boxscores/?month=10&day=5&year=2020
      

      【讨论】:

        猜你喜欢
        • 2015-01-27
        • 2018-11-04
        • 2021-12-30
        • 2020-03-03
        • 2019-08-27
        • 2012-10-28
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多