2017-01-05 18 views
1

こんにちは、premierleagueウェブサイトからテーブルを抽出しようとしています。ウェブサイトからhtmlテーブルを抽出する

私が使用していますパッケージには、次のようにrvestパッケージと私はinital相に使用していたコードは次のとおりです。

library(rvest) 
library(magrittr) 
premierleague <- read_html("https://fantasy.premierleague.com/a/entry/767830/history") 
premierleague %>% html_nodes("ism-table") 

私はrvestためhtml_nodesを抽出するために働くだろうhtmlタグを見つけることができませんでしたパッケージ。

"http://admissions.calpoly.edu/prospective/profile.html"のデータを抽出するのにも同様の方法を使用していましたが、データを抽出できました。次のように私はcalpolyのために使用されるコードは次のとおりです。fantasy.premierleague.comからデータを取得する上の任意のヘルプは高く評価されてhttps://www.youtube.com/watch?v=gSbuwYdNYLM&ab_channel=EvanO%27Brien

library(rvest) 
library(magrittr) 
CPadmissions <- read_html("http://admissions.calpoly.edu/prospective/profile.html") 

CPadmissions %>% html_nodes("table") %>% 
    .[[1]] %>% 
    html_table() 

は、このリンクを介してユーチューブから上記のコードを手に入れました。何らかのAPIを使用する必要がありますか?

+1

このためには、何らかのAPIまたは['RSelenium'](https://cran.r-project.org/web/packages/RSelenium/vignettes/RSelenium-basics.html)を使用する必要があります動的に生成される。したがって、静的なサイトで動作するライブラリを使用するのは難しいです。 – Abdou

+0

私はRseleniumを初めて使用しています。私が従うサンプルスクリプトを投稿できますか? –

答えて

7

データは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_if(is.character, parse_number)はかなりうまくいくでしょう。)矢印は最後の列がすべてNAであるイメージですが、とにかく計算できます。

+0

「Phantom()のエラー:PhantomJSバイナリが見つかりません」というエラーが表示されます。 'ptm < - phantom()'で置き換えます。どんな考えが起こっているの?私はダウンロードしたphantomJSを持っています –

+0

ええ、最初にインストールする必要があります。 Windowsでは、 'webshot :: install_phantomjs'があなたのためにそれを行います。 macOSでは、[homebrew](http://brew.sh/)は素晴らしく、どのシステムでも[手動でインストールする]ことができます(http://phantomjs.org/)。 – alistaire

+0

ええ、私は自宅で問題があり、バイナリファイルを/ usr/local/bin /に追加しましたが、何も変わりませんでした。 –

1

このソリューションでは、RSeleniumとパッケージXMLを使用しています。また、firefoxと正しく動作するRSeleniumの作業インストールがあることを前提としています。 firefoxのスタータースクリプトパスをPATHに追加してください。

OS Xを使用している場合は、PATH/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))) 

これは得られるはず:

GW GP PB GR  TM TC OP OR Value CPW 
GW1 34 1 2406373 0 0 34 2406373 100.0  
GW2 47 6 2659789 0 0 81 2448674 100.0  
GW3 53 9 541258 2 0 134 1914025 99.9  
GW4 51 7 905524 0 0 185 1461665 100.0  
GW5 66 14 379438 3 4 247 958889 100.1  
GW6 66 2 303704 2 4 309 510376 99.9  
GW7 65 9 138792 2 4 370 232474 99.8  
GW8 63 3 108363 0 0 433 87967 100.4  
GW9 48 8 1114609 2 0 481 75385 100.9  
GW10 90 2 71210 0 0 571 27716 101.1  
GW11 71 2 421706 3 4 638 16083 100.9  
GW12 35 9 2798661 2 4 669 31820 101.2  
GW13 41 8 2738535 1 0 710 53487 101.1  
GW14 82 15 308725 0 0 792 29436 100.2  
GW15 55 9 1048808 2 4 843 29399 100.6  
GW16 49 8 1801549 0 0 892 35142 100.7  
GW17 48 4 2116706 2 0 940 40857 100.7  
GW18 42 2 3315031 0 0 982 78136 100.8  
GW19 41 9 2600618 0 0 1023 99048 100.6  
GW20 53 0 1644385 0 0 1076 113148 100.8 

が列CPW(前の週からの変化)であることに注意してください空の文字列のベクトル。

こちらがお役に立てば幸いです。

関連する問題