2017-03-27 1 views
2

RVest를 사용하여 블로그 텍스트를 긁어 모으고 특정 노드를 제외하는 간단한 방법을 고민하고 있습니다.노드 제외 RVest

AllandSundry_test <- read_html 
("http://www.sundrymourning.com/2017/03/03/lets-go-back-to-commenting-on-the-weather/") 

testpost <- AllandSundry_test %>% 
html_node("#contentmiddle") %>% 
html_text() %>% 
as.character() 

내가 ID의 "contenttitle"와 "commentblock"와 함께 두 개의 노드를 제외 할 : 다음은 텍스트를 가져옵니다. 아래에서는 'commentblock'태그를 사용하여 주석 만 제외합니다.

이 스크립트를 실행하면 그 결과는 단순히 날짜가되며 나머지 텍스트는 모두 사라집니다. 어떤 제안?

나는 답을 찾기 위해 많은 시간을 보냈지 만, R (그리고 html)을 처음 사용하기 때문에 이것이 확실한 것이면 양해 해 주셔서 감사합니다.

+0

당신이 긁어 모으고 싶었던 URL을 제공해 주시겠습니까? 나는 당신의 질문의 요지를 이해하지 못했습니다. – Bharath

+0

응답 해 주셔서 감사합니다. 내가 사용하고있는 정확한 예를 사용하여 질문을 편집했습니다. 도와 주셔서 감사합니다. –

답변

1

거의 다 왔었습니다. html_node 대신 html_nodes을 사용해야합니다.

html_node은 첫 번째 요소를 검색하고 html_nodes은 페이지의 각 요소를 목록으로 반환합니다.
toString() 함수는 문자열 목록을 하나로 축소합니다.

library(rvest) 

AllandSundry_test <- read_html("http://www.sundrymourning.com/2017/03/03/lets-go-back-to-commenting-on-the-weather/") 

testpost <- AllandSundry_test %>% 
    html_nodes("#contentmiddle>:not(#commentblock)") %>% 
    html_text %>% 
    as.character %>% 
    toString 

testpost 
#> [1] "\n\t\tMar\n\t\t3\n\t, Mar, 3, \n\t\tLet's go back to 
#> commenting on the weather\n\t\t\n\t\t, Let's go back to commenting on 
#> the weather, Let's go back to commenting on the weather, I have just 
#> returned from the grocery store, and I need to get something off my chest. 
#> When did "Got any big plans for the rest of the day?" become 
#> the default small ...<truncated> 

당신은 여전히 ​​캐릭터를 조금 정리할 필요가있다.

+0

웬일인지, 그것은 아직도 설명을 집어 들고있는 것처럼 보인다. 고마워,이 특정 웹 사이트의 HTML은 지저분 해 보입니다. –

+0

네 말이 맞다. 내가 정말로 확인하지 않았 음을 확신했다. 왜 이것이 작동하지 않는지 나는 지금 확실하지 않다. 어쨌든, 해결책을 통해 대답을 편집하십시오. – GGamba

+0

고마워요! 매력처럼 작동합니다. –

관련 문제