&을 읽으 려하고 동물 보호소에서 동물을위한 조건 목록을 얻기 위해 HTML을 파싱했습니다. HTML 파싱에 익숙하지 않은 사람이 도움이되지 않는다고 확신하지만, 어디에서 빨리 빠져 나가고있는 것처럼 보입니다.R HTML을 읽기 및 구문 분석
다음은 HTML의 코드 조각입니다 :
는<select multiple="true" name="asilomarCondition" id="asilomarCondition">
<option value="101">
Behavior- Aggression, Confrontational-Toward People (mild)
-
TM</option>
....
</select>
<select...>
만 하나 개의 태그있다 나머지는 모두 <option value=x>
이다.
저는 XML 라이브러리를 사용해 왔습니다. 내가 줄 바꿈과 탭을 제거 할 수 있지만 태그 제거 어떤 성공 없었어요 : 최종 결과
conditions.html <- paste(readLines("Data/evalconditions.txt"), collapse="\n")
conditions.text <- gsub('[\t\n]',"",conditions.html)
을, 나는 나중에 사용하기 위해 추가로 처리 할 수있는 모든 조건의 목록을하고 싶습니다 요소 이름으로 :
Behavior- Aggression, Confrontational-Toward People (mild)-TM
Behavior- Aggression, Confrontational-Toward People (moderate/severe)-UU
...
내가 gsub
패턴이 충분할 것이다 경우 XML 라이브러리 (또는 다른 라이브러리)을 사용하거나 할 필요가 있는지 확실하지 않습니다 (어느 쪽이든, 나는 그것을 사용하는 방법을 해결하기 위해 필요) .
library(rvest)
#read the html page
page<-read_html("test.html")
#get the text from the "option" nodes and then trim the whitespace
nodes<-trimws(html_text(html_nodes(page, "option")))
#nodes will need additional clean up to remove the excessive spaces
#and newline characters
nodes<-gsub("\n", "", nodes)
nodes<-gsub(" ", "", nodes)
벡터 노드가 요청한 결과되어야합니다 :
해당 선택 상자를 사용하여 전체 URL을 가리 키거나 스 니펫을 약간 확장 할 수 있습니까? – hrbrmstr
rvest 패키지를 사용하기가 더 쉽습니다. 웹 사이트에 대한 링크를 제공 할 수 있다면 누군가가 당신의 솔루션을 코딩 할 수 있습니다. – Dave2e
HTML입니다. @alistaire 형식의 선택 목록 – hrbrmstr