2017-03-27 8 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」の2つのノードを除外します。以下では、タグ "commentblock"を使用してコメントだけを除外してみます。

これを実行すると、結果は単に日付になります。残りのテキストはすべて削除されます。助言がありますか?

私は答えを探すのに多くの時間を費やしましたが、私はR(およびhtml)を初めて使っています。これが明らかなことがあれば、辛抱強くお礼申し上げます。

+0

あなたが掻き出したいURLを入力してください。私はちょうどあなたの質問の要点を理解できませんでした。 – Bharath

+0

お返事ありがとうございます。私は使用している正確な例で質問を編集しました。私はあなたの助けに感謝します。 –

答えて

1

あなたはほとんどそこにいました。 html_nodeの代わりにhtml_nodesを使用してください。

html_nodeは、遭遇する最初の要素を取得し、html_nodesは、ページ内の一致する各要素をリストとして返します。
toString()関数は、文字列のリストを1つにまとめます。

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

ありがとう!魅力のように動作します。 –

関連する問題