2017-02-17 1 views
0

R의 새로운 점을 설명하기 위해 최선을 다할 것입니다. "rvest"패키지를 사용하여 데이터 스크래핑을했습니다. 이 예에서는 Wikipedia의 테이블에서 미국 주 인구를 긁어 모으고 있습니다. 내가 사용하는 코드는 다음과 같습니다rvest 출력을 테이블로 변환하는 방법

[2] "7000100000000000000♠1"           
[3] " California"             
[4] "39,250,017"             
[5] "37,254,503"             
[6] "7001530000000000000♠53"          
[7] "738,581"              
[8] "702,905"              
[9] "12.15%"              
[10] "7000200000000000000♠2"           
[11] "7000200000000000000♠2"           
[12] " Texas"              
[13] "27,862,596"             
[14] "25,146,105"             
[15] "7001360000000000000♠36"          
[16] "763,031"              
[17] "698,487"              
[18] "8.62%" 

어떻게 그것을가 표시되는 방식과 유사을 설정 테이블에 텍스트의 이러한 문자열을 설정할 수 있습니다 다음과 같이

library(rvest) 
statepop = read_html("https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population") 
forecasthtml = html_nodes(statepop, "td") 
forecasttext = html_text(forecasthtml) 
forecasttext 

결과 출력했다 원래 위키 백과 페이지 (열, 행 등)?

+1

'html_text()'대신'html_table()'을 사용하십시오. – Nate

답변

2

rvest의 html_table 함수를 사용해보십시오.
참고 페이지에 5 개의 테이블이 있으므로 구문 분석 할 테이블을 지정해야합니다.

library(rvest) 

statepop = read_html("https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population") 
#find all of the tables on the page 
tables<-html_nodes(statepop, "table") 
#convert the first table into a dataframe 
table1<-html_table(tables[1]) 
관련 문제