【问题标题】:Struggling to get scraped data from a single column into proper table format努力将单列中的抓取数据转换为正确的表格格式
【发布时间】:2021-05-22 19:54:06
【问题描述】:

我正在尝试从 rotoguru1.com 抓取与梦幻足球运动员薪水相关的数据。我试图从中收集数据的示例网页可以在这里找到:http://rotoguru1.com/cgi-bin/fyday.pl?week=1&year=2014&game=dk&scsv=1。数据以 scsv 格式方便地在 html“pre”标签下的每个页面上提供。我首先使用一个 for 循环来生成我想要从中抓取数据的所有 url,但随后我努力将这些网页中的所有数据转换为我想要的格式,最终的数据表包含所有抓取的数据。我使用第二个 for 循环遍历所有 url,在每个页面上使用 read_html() 函数,然后使用 html_nodes('pre')%>%html_text() 提取感兴趣的数据。问题是,由于我的代码当前有效,这只是为每个页面创建一个大对象,其中包含整个 scsv 作为单个对象,而不是作为包含单个列(周、年、gid、名称、pos、团队、h/a、opt、dk 点数、dk 工资)。相反,我想要一个包含我试图抓取的所有页面的这些单独列的数据表,但对网络抓取没有太多经验,也不知道如何解决这个问题。任何帮助将不胜感激。以下是我迄今为止编写的代码:

library(purrr) 
library(rvest)
library(data.table)
library(stringr)
library(tidyr)


#Declare variables and empty data tables
path1<-("http://rotoguru1.com/cgi-bin/fyday.pl?week=")
seasons<-c("2014", "2015", "2016","2017","2018","2019","2020")
weeks<-1:17
result<-NULL
temp<-NULL

#Use nested for loops to get the url, season, and week for each webpage of interest, store in result data table
for(s in 1:length(seasons)){
  for(w in 1:length(weeks)){
    temp<- paste0(path1, as.character(w),"&year=",seasons[s],"&game=dk&scsv=1")
    result<-rbind(result,temp)
  }
}

#Get rid of any potential empty values from result
result<-compact(result) 

final<-data.table()
#Create final data table with all injury information
for (i in 1:length(result)){
  page<-read_html(result[i])
  data<-page%>%html_nodes("pre")%>%html_text()
  final<-rbind(data,final)
  
}

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    该页面具有获取 html 表格格式的选项,因此您可以在循环中使用 "&amp;game=dk" 而不是 "&amp;game=dk&amp;scsv=1"

    然后只是措辞html_table

    这里是一个页面的例子

    page<-read_html(result[1])
    
    x<-data.frame(page%>%html_nodes("table")  %>%  `[`(9) %>% html_table(T))
    colnames(x)  <- as.character(x[1,])
    x <- x[-1,]
    

    【讨论】:

      【解决方案2】:

      我相信您从第一个for-loop cn 开始的整个代码都可以替换为以下(主要是data.table)解决方案:

      result <- CJ(seasons, weeks)[, paste0(path1, weeks, "&year=", seasons, "&game=dk&scsv=1") ]
      #loop over result
      final <- data.table::rbindlist(
        lapply( result, function(x) {
          read_html(x) %>%
            html_nodes("pre") %>% 
            html_text() %>%
            data.table::fread( sep = ";" ) # <-- !!
          } ),
        use.names = TRUE, fill = TRUE )
      

      【讨论】:

      • 非常感谢您的解决方案,成功了!
      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多