2016-10-26 5 views
3

複数のURLを実行して各テーブルのデータをスクラップし、すべてを1つのデータフレームに連結する動的ループを作成しようとしています。私は以下に示すようにいくつかのアイデアを試しましたが、これまでに何もできませんでした。この種のものは実際に私の運転手ではありませんが、私はこれがどのように機能するかを学ぼうとしています。もし誰かがこれをやるのを助けることができたら、私は本当にそれを感謝します。HTMLテーブルをループしてデータフレームを作成しようとしています

ありがとうございます。

静的URL: http://www.nfl.com/draft/2015/tracker?icampaign=draft-sub_nav_bar-drafteventpage-tracker#dt-tabs:dt-by-position/dt-by-position-input:qb

library(rvest) 

#create a master dataframe to store all of the results 
complete<-data.frame() 

yearsVector <- c("2010", "2011", "2012", "2013", "2014", "2015") 
positionVector <- c("qb", "rb", "wr", "te", "ol", "dl", "lb", "cb", "s") 
for (i in 1:length(yearsVector)) 
{ 
    for (j in 1:length(positionVector)) 
    { 
    # create a url template 
    URL.base<-"http://www.nfl.com/draft/" 
    URL.intermediate <- "/tracker?icampaign=draft-sub_nav_bar-drafteventpage-tracker#dt-tabs:dt-by-position/dt-by-position-input:" 
    #create the dataframe with the dynamic values 
    URL <- paste0(URL.base, yearsVector, URL.intermediate, positionVector) 
    #print(URL) 

    #read the page - store the page to make debugging easier 
    page<- read_html(URL) 

    #This needs work since the page is dynamicly generated. 
    DF <- html_nodes(page, xpath = ".//table") %>% html_table(fill=TRUE) 
    #About 530 names returned, may need to search and extracted requested info. 



    # to find the players last names 
    lastnames<-str_locate_all(page, "lastName")[[1]] 
    names<- str_sub(page, lastnames[,2]+4, lastnames[,2]+20) 
    names<-str_extract(names, "[A-Z][a-zA-Z]*") 

    length(names[-c(1:16)]) 
    #Still need to delete the first 16 names (don't know if this is consistent across all years 

    #to find the players positions 
    positions<-str_locate_all(page, "pos")[[1]] 
    ppositions<- str_sub(page, positions[,2]+4, positions[,2]+10) 
    pos<-str_extract(ppositions, "[A-Z]*") 

    pos<- pos[pos !=""] 
    #Still need to clean delete the first 16 names (don't know if this is consistent across all years 


    #store the temp values into the master dataframe 
    complete<-rbind(complete, DF) 
    } 
} 

私はあなたのコードデイブを組み込むために、私のOPを編集しました。私はほとんどそこにいると思うが、ここには何かがない。私はこのエラーが発生しています。 evalの中

エラー(代替(expr)は、ENVIR、enclos):単一の値

を期待しては、私が知っているURLは正しいです!

http://postimg.org/image/ccmvmnijr/

私はこの問題は、この行であると思う:

page <- read_html(URL) 

それとも、多分この行:

DF <- html_nodes(page, xpath = ".//table") %>% html_table(fill = TRUE) 

あなたは私がここでフィニッシュラインを介して得るのを助けることができますか?ありがとう!

答えて

0

この回答をお試しください! URLの作成を修正し、要求された情報を保存するためのマスターデータフレームを設定しました。ページが動的に生成されるので、これらを使用すると、rvestの標準ツールは動作しません。選手(約16欄)、カレッジ、ピック情報はすべてページに保存されています。それを探して抽出することです。

library(rvest) 
library(stringr) 
library(dplyr) 

#create a master dataframe to store all of the results 
complete<-data.frame() 

yearsVector <- c("2011", "2012", "2013", "2014", "2015") 
#all position information is stored on each page no need to create sparate queries 
#positionVector <- c("qb", "rb", "wr", "te", "ol", "dl", "lb", "cb", "s") 
positionVector <- c("qb") 
for (i in 1:length(yearsVector)) 
{ 
    for (j in 1:length(positionVector)) 
    { 
    # create a url template 
    URL.base<-"http://www.nfl.com/draft/" 
    URL.intermediate <- "/tracker?icampaign=draft-sub_nav_bar-drafteventpage-tracker#dt-tabs:dt-by-position/dt-by-position-input:" 
    #create the dataframe with the dynamic values 
    URL <- paste0(URL.base, yearsVector[i], URL.intermediate, positionVector[j]) 
    print(yearsVector[i]) 
    print(URL) 

    #read the page - store the page to make debugging easier 
    page<- read_html(URL) 

    #This needs work since the page is dynamicly generated. 
    #DF <- html_nodes(page, xpath = ".//table") %>% html_table(fill=TRUE) 
    #About 539 names returned, may need to search and extracted requested info. 
    #find records for each player 
    playersloc<-str_locate_all(page, "\\{\"personId.*?\\}")[[1]] 
    players<-str_sub(page, playersloc[,1]+1, playersloc[,2]-1) 
    #fix the cases where the players are named Jr. 
    players<-gsub(", ", "_", players ) 

    #split and reshape the data in a data frame 
    play2<-strsplit(gsub("\"", "", players), ',') 
    data<-sapply(strsplit(unlist(play2), ":"), FUN=function(x){x[2]}) 
    df<-data.frame(matrix(data, ncol=16, byrow=TRUE)) 
    #name the column names 
    names(df)<-sapply(strsplit(unlist(play2[1]), ":"), FUN=function(x){x[1]}) 

    #sort out the pick information 
    picks<-str_locate_all(page, "\\{\"id.*?player.*?\\}")[[1]] 
    picks<-str_sub(page, picks[,1]+1, picks[,2]-1) 
    #fix the cases where there are commas in the notes section. 
    picks<-gsub(", ", "_", picks ) 
    picks<-strsplit(gsub("\"", "", picks), ',') 
    data<-sapply(strsplit(unlist(picks), ":"), FUN=function(x){x[2]}) 
    picksdf<-data.frame(matrix(data, ncol=6, byrow=TRUE)) 
    names(picksdf)<-sapply(strsplit(unlist(picks[1]), ":"), FUN=function(x){x[1]}) 

    #sort out the college information 
    schools<-str_locate_all(page, "\\{\"id.*?name.*?\\}")[[1]] 
    schools<-str_sub(page, schools[,1]+1, schools[,2]-1) 
    schools<-strsplit(gsub("\"", "", schools), ',') 
    data<-sapply(strsplit(unlist(schools), ":"), FUN=function(x){x[2]}) 
    schoolsdf<-data.frame(matrix(data, ncol=3, byrow=TRUE)) 
    names(schoolsdf)<-sapply(strsplit(unlist(schools[1]), ":"), FUN=function(x){x[1]}) 

    #merge the 3 tables together 
    df<-left_join(df, schoolsdf, by=c("college" = "id")) 
    df<-left_join(df, picksdf, by=c("pick" = "id")) 

    #store the temp values into the master dataframe 
    complete<-rbind(complete, df) 
    } 
} 

正しい正規表現を見つけて必要な情報を見つけて見つけるのは難しい作業でした。 2010年のデータは、大学の情報を使用する別の形式を使用するように見えるので、私はその年を残しました。 また、このサイトの利用規約に違反していないことを確認してください。 幸運

+0

これは視覚的には素晴らしいようですが、私のためには実行されません。 'エラー:単一の値を期待しています' 私はサービス違反の条件を認識しておらず、これを定期的に使用するつもりはありません。私は単にそのコンセプトを想像し、この仕組みを学べば、他の多くの(関連性のある、無関係な)ものを手伝ってくれます。 もう一度、スクリプトは私のために機能しません。私はMS Visual StudioとR Studioでこれを試しました。上記と同じ結果が得られました。このスクリプトはどのように動作させることができますか? –

+0

上記の改訂コードが有効です。 – Dave2e

+0

WOW !!非常に素晴らしい!!私はこのコードに完全に必要です。私はそれの約3/4を理解していますが、間違いなくそのすべてではありません。 これがどのように機能するかを私に見せてくれてありがとう。 –

関連する問題