gpt4 book ai didi

r - 在 Wiki 的网球 table 上使用 Rvest 进行网页抓取

转载 作者:行者123 更新时间:2023-12-05 04:12:57 25 4
gpt4 key购买 nike

我是 R 的初学者。我想了解更多关于 rvest 以及如何从网络上抓取的信息。这是 wiki 页面 ( https://en.wikipedia.org/wiki/Andy_Murray ),下面是我要转移到 R 的表格。

table that i want

使用 CSS 选择器,我发现特定表格位于“.wikitable”上。按照其他网页上的一些教程,这是我使用的代码:

library(rvest)
tennis <- read_html("https://en.wikipedia.org/wiki/Andy_Murray")
trial <- tennis %>% html_nodes(".wikitable") %>% html_table(fill = T)
trial

我无法将结果隔离到我想要的表中。有人可以教我怎么做吗?另一件事,管道是做什么的(%>%)?

最佳答案

你快到了。您提取的是一个列表。要获得所需的元素,您需要使用索引:

trial[[2]]

要进一步清洁它,请使用:

df <- trial[[2]]
df <- df[-1,]
df[,17:20] <- NULL

enter image description here

%>% 被称为来自 magrittr/dplyr 包的管道。更多信息 here .

关于r - 在 Wiki 的网球 table 上使用 Rvest 进行网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com