Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的网站提取html表_R_Html Table_Rvest - Fatal编程技术网

从R中的网站提取html表

从R中的网站提取html表,r,html-table,rvest,R,Html Table,Rvest,嗨,我正试图从英超联赛网站上提取表格 我使用的包是rvest包,我在初始阶段使用的代码如下: library(rvest) library(magrittr) premierleague <- read_html("https://fantasy.premierleague.com/a/entry/767830/history") premierleague %>% html_nodes("ism-table") library(rvest) library(magrittr) C

嗨,我正试图从英超联赛网站上提取表格

我使用的包是rvest包,我在初始阶段使用的代码如下:

library(rvest)
library(magrittr)
premierleague <- read_html("https://fantasy.premierleague.com/a/entry/767830/history")
premierleague %>% html_nodes("ism-table")
library(rvest)
library(magrittr)
CPadmissions <- read_html("http://admissions.calpoly.edu/prospective/profile.html")

CPadmissions %>% html_nodes("table") %>%
  .[[1]] %>%
  html_table()
通过此链接从youtube获取上述代码:


非常感谢您对从fantasy.premireleague.com获取数据的任何帮助。我需要使用某种API吗

因为数据是用JavaScript加载的,所以用rvest获取HTML并不能得到您想要的东西,但是如果您在RSelenium中使用PhantomJS作为无头浏览器,那么RSelenium标准并不那么复杂:

library(RSelenium)
library(rvest)

# initialize browser and driver with RSelenium
ptm <- phantom()
rd <- remoteDriver(browserName = 'phantomjs')
rd$open()

# grab source for page
rd$navigate('https://fantasy.premierleague.com/a/entry/767830/history')
html <- rd$getPageSource()[[1]]

# clean up
rd$close()
ptm$stop()

# parse with rvest
df <- html %>% read_html() %>% 
    html_node('#ismr-event-history table.ism-table') %>% 
    html_table() %>% 
    setNames(gsub('\\S+\\s+(\\S+)', '\\1', names(.))) %>%    # clean column names
    setNames(gsub('\\s', '_', names(.)))

str(df)
## 'data.frame':    20 obs. of  10 variables:
##  $ Gameweek                : chr  "GW1" "GW2" "GW3" "GW4" ...
##  $ Gameweek_Points         : int  34 47 53 51 66 66 65 63 48 90 ...
##  $ Points_Bench            : int  1 6 9 7 14 2 9 3 8 2 ...
##  $ Gameweek_Rank           : chr  "2,406,373" "2,659,789" "541,258" "905,524" ...
##  $ Transfers_Made          : int  0 0 2 0 3 2 2 0 2 0 ...
##  $ Transfers_Cost          : int  0 0 0 0 4 4 4 0 0 0 ...
##  $ Overall_Points          : chr  "34" "81" "134" "185" ...
##  $ Overall_Rank            : chr  "2,406,373" "2,448,674" "1,914,025" "1,461,665" ...
##  $ Value                   : chr  "£100.0" "£100.0" "£99.9" "£100.0" ...
##  $ Change_Previous_Gameweek: logi  NA NA NA NA NA NA ...

像往常一样,更多的清洁是必要的,但总的来说,它的形状很好,没有太多的工作。如果您使用的是tidyverse,df%>%mutate\u ifis.character,parse\u number将非常适合。箭头是图像,这就是为什么最后一列都是NA的原因,但您仍然可以计算它们。

因为数据是用JavaScript加载的,用rvest获取HTML不会得到您想要的,但是如果您在RSelenium中使用PhantomJS作为无头浏览器,则RSelenium标准并不那么复杂:

library(RSelenium)
library(rvest)

# initialize browser and driver with RSelenium
ptm <- phantom()
rd <- remoteDriver(browserName = 'phantomjs')
rd$open()

# grab source for page
rd$navigate('https://fantasy.premierleague.com/a/entry/767830/history')
html <- rd$getPageSource()[[1]]

# clean up
rd$close()
ptm$stop()

# parse with rvest
df <- html %>% read_html() %>% 
    html_node('#ismr-event-history table.ism-table') %>% 
    html_table() %>% 
    setNames(gsub('\\S+\\s+(\\S+)', '\\1', names(.))) %>%    # clean column names
    setNames(gsub('\\s', '_', names(.)))

str(df)
## 'data.frame':    20 obs. of  10 variables:
##  $ Gameweek                : chr  "GW1" "GW2" "GW3" "GW4" ...
##  $ Gameweek_Points         : int  34 47 53 51 66 66 65 63 48 90 ...
##  $ Points_Bench            : int  1 6 9 7 14 2 9 3 8 2 ...
##  $ Gameweek_Rank           : chr  "2,406,373" "2,659,789" "541,258" "905,524" ...
##  $ Transfers_Made          : int  0 0 2 0 3 2 2 0 2 0 ...
##  $ Transfers_Cost          : int  0 0 0 0 4 4 4 0 0 0 ...
##  $ Overall_Points          : chr  "34" "81" "134" "185" ...
##  $ Overall_Rank            : chr  "2,406,373" "2,448,674" "1,914,025" "1,461,665" ...
##  $ Value                   : chr  "£100.0" "£100.0" "£99.9" "£100.0" ...
##  $ Change_Previous_Gameweek: logi  NA NA NA NA NA NA ...

像往常一样,更多的清洁是必要的,但总的来说,它的形状很好,没有太多的工作。如果您使用的是tidyverse,df%>%mutate\u ifis.character,parse\u number将非常适合。箭头是图像,这就是为什么最后一列都是NA,但您仍然可以计算它们。

此解决方案使用RSelenium和包XML。它还假设您有一个可以正常使用firefox的RSelenium安装。只需确保已将firefox初学者脚本路径添加到路径中

如果您使用的是OS X,则需要将/Applications/Firefox.app/Contents/MacOS/添加到路径中。或者,如果你在Ubuntu机器上,它可能是/usr/lib/firefox/。一旦您确定这是可行的,您可以通过以下操作转到R:

# Install RSelenium and XML for R
#install.packages("RSelenium")
#install.packages("XML")

# Import packages
library(RSelenium)
library(XML)

# Check and start servers for Selenium
checkForServer()
startServer()

# Use firefox as a browser and a port that's not used
remote_driver <- remoteDriver(browserName="firefox", port=4444)
remote_driver$open(silent=T)

# Use RSelenium to browse the site
epl_link <- "https://fantasy.premierleague.com/a/entry/767830/history"
remote_driver$navigate(epl_link)
elem <- remote_driver$findElement(using="class", value="ism-table")

# Get the HTML source
elemtxt <- elem$getElementAttribute("outerHTML")

# Use the XML package to work with the HTML source
elem_html <- htmlTreeParse(elemtxt, useInternalNodes = T, asText = TRUE)

# Convert the table into a dataframe
games_table <- readHTMLTable(elem_html, header = T, stringsAsFactors = FALSE)[[1]]

# Change the column names into something legible
names(games_table) <- unlist(lapply(strsplit(names(games_table), split = "\\n\\s+"), function(x) x[2]))
names(games_table) <- gsub("£", "Value", gsub("#", "CPW", gsub("Â","",names(games_table))))

# Convert the fields into numeric values
games_table <- transform(games_table, GR = as.numeric(gsub(",","",GR)),
                    OP = as.numeric(gsub(",","",OP)),
                    OR = as.numeric(gsub(",","",OR)),
                    Value = as.numeric(gsub("£","",Value)))
请注意,前一周的CPW change列是一个空字符串向量


我希望这会有所帮助。

此解决方案使用RSelenium和包XML。它还假设您有一个可以正常使用firefox的RSelenium安装。只需确保已将firefox初学者脚本路径添加到路径中

如果您使用的是OS X,则需要将/Applications/Firefox.app/Contents/MacOS/添加到路径中。或者,如果你在Ubuntu机器上,它可能是/usr/lib/firefox/。一旦您确定这是可行的,您可以通过以下操作转到R:

# Install RSelenium and XML for R
#install.packages("RSelenium")
#install.packages("XML")

# Import packages
library(RSelenium)
library(XML)

# Check and start servers for Selenium
checkForServer()
startServer()

# Use firefox as a browser and a port that's not used
remote_driver <- remoteDriver(browserName="firefox", port=4444)
remote_driver$open(silent=T)

# Use RSelenium to browse the site
epl_link <- "https://fantasy.premierleague.com/a/entry/767830/history"
remote_driver$navigate(epl_link)
elem <- remote_driver$findElement(using="class", value="ism-table")

# Get the HTML source
elemtxt <- elem$getElementAttribute("outerHTML")

# Use the XML package to work with the HTML source
elem_html <- htmlTreeParse(elemtxt, useInternalNodes = T, asText = TRUE)

# Convert the table into a dataframe
games_table <- readHTMLTable(elem_html, header = T, stringsAsFactors = FALSE)[[1]]

# Change the column names into something legible
names(games_table) <- unlist(lapply(strsplit(names(games_table), split = "\\n\\s+"), function(x) x[2]))
names(games_table) <- gsub("£", "Value", gsub("#", "CPW", gsub("Â","",names(games_table))))

# Convert the fields into numeric values
games_table <- transform(games_table, GR = as.numeric(gsub(",","",GR)),
                    OP = as.numeric(gsub(",","",OP)),
                    OR = as.numeric(gsub(",","",OR)),
                    Value = as.numeric(gsub("£","",Value)))
请注意,前一周的CPW change列是一个空字符串向量


我希望这会有所帮助。

您需要使用某种API或用于此目的,因为页面是动态生成的。所以,对于一个使用静态站点的库来说,很难做到这一点。您可以发布一个我可以遵循的示例脚本吗?您将需要使用某种API或用于此,因为页面是动态生成的。所以,对于一个使用静态站点的库来说,很难做到这一点。你能发布一个我可以遵循的示例脚本吗?我在phantom中遇到一个错误:PhantomJS二进制文件未找到。有了ptm,你必须先安装它。在Windows上,webshot::install_phantomjs将为您完成这项工作;在macOS上,很好,或者在任何系统上,你都可以。是的,我在自制软件上遇到了问题,我将二进制文件添加到了/usr/local/bin/中,但没有任何更改。这很奇怪;它没有任何依赖项,并且您不应该在/usr/local/bin中出现路径问题。它是如何失败的?phantomjs-version在命令提示符下工作吗?塞勒尼姆有一个能为你指明正确方向的东西。@CyrusMohammadian Odd。您可以通过Selenium服务器来控制PhantomJS,而不是它自己的webdriver模式。请参阅vignette的第二部分。有了ptm,你必须先安装它。在Windows上,webshot::install_phantomjs将为您完成这项工作;在macOS上,很好,或者在任何系统上,你都可以。是的,我在自制软件上遇到了问题,我将二进制文件添加到了/usr/local/bin/中,但没有任何更改。这很奇怪;它没有任何依赖项,并且您不应该在/usr/local/bin中出现路径问题。它是如何失败的?phantomjs-version在命令提示符下工作吗?塞勒尼姆有一个能为你指明正确方向的东西。@CyrusMohammadian Odd。您可以通过Selenium服务器来控制PhantomJS,而不是它自己的webdriver模式。请参阅本文的第二部分。