如何用worldfootballR包从fbref获取欧冠赛事数据?
解决方法
正确调用fb_match_results的方式
之前的代码冗余参数导致函数解析出错,直接传入目标赛事的URL即可,不需要额外填country或season_end_year:
library(worldfootballR) # 仅传入非国内联赛的赛程URL cl_data <- fb_match_results( non_dom_league_url = "https://fbref.com/en/comps/8/2022-2023/schedule/2022-2023-Champions-League-Scores-and-Fixtures" ) # 提取你需要的字段:主客队、比分、预期进球 target_data <- cl_data[, c("Home", "Away", "Score", "xG", "xG.1")] # 重命名列让逻辑更清晰 colnames(target_data) <- c("主队", "客队", "全场比分", "主队预期进球", "客队预期进球") print(target_data)
出错原因说明
- 你之前传入的
country = ''和season_end_year = '2023'属于国内联赛的匹配参数,会干扰函数对洲际赛事URL的解析,函数会优先走国内联赛的赛季匹配逻辑,导致无法读取传入的URL数据。 fb_match_urls用country = "UEFA"不合适,这个参数是给具体国家联赛用的,欧冠属于洲际赛事,不适用这个参数。
备选方案(如果worldfootballR调用仍有问题)
可以直接用rvest包解析网页获取数据:
library(rvest) url <- "https://fbref.com/en/comps/8/2022-2023/schedule/2022-2023-Champions-League-Scores-and-Fixtures" page <- read_html(url) # 定位并提取赛程表格 schedule_table <- page %>% html_element("table#sched_2022-2023_8_1") %>% html_table(fill = TRUE) # 筛选需要的列并整理 clean_table <- schedule_table[, c("Home", "Away", "Score", "xG", "xG.1")] colnames(clean_table) <- c("主队", "客队", "全场比分", "主队预期进球", "客队预期进球") print(clean_table)
内容的提问来源于stack exchange,提问作者Juan
相关产品推荐
相关产品推荐

