2017-10-22 8 views
1

rvestを使用してテーブルと標準HTMLを読むのに成功しました。私はそれに複数の引用符を持つテキストを読むことで、現時点で問題を抱えています。 rvestは、引用符で囲まれたテキストとスペースの後に引用符で囲まれたテキストの新しい行が表示されたときに、新しい文字(a-z)を追加するようです。rvestを使って奇妙に引用されたテキストを読む

これは再現可能な例です。

library(rvest) 
read_html("https://www.lds.org/scriptures/ot/gen/1?lang=eng") %>% 
    html_node("#p3") %>% 
    html_text() 

結果が "asaid" と "枯れ" である

"3 And God asaid, Let there be blight: and there was light." 

スペルミスです。 lol

詳細については、Webインスペクタを使用してhtml構造を調べました。

<p class="verse" id="p3> 
<span class="verse-number verse">3</span> 
"And God " 
"said" 
", Let there be " 
"light" 
": and there was light." 
</p> 

このようなフォーマットの悪いテキストの解決方法は何か不思議です。

+0

問題ここ(一番下までスクロールし、「ショーの脚注」をクリックしてください。それを解決するためにどのように、私はまだ働いている2脚注、デフォルトで隠されている「a」および「b」があるということですその上に。 – neilfws

答えて

1

「脚注を表示する」を探してクリックした場合は、もう一度ページを調べると問題が表示されます。 「asaid」の余分な文字「a」と「blight」の「b」は、supタグで囲まれた隠れた脚注のテキストです。

page <- read_html("https://www.lds.org/scriptures/ot/gen/1?lang=eng") 
page %>% 
    html_nodes(xpath = "//p[@id = 'p3']") %>% 
    html_structure() 

[[1]] 
<p#p3 .verse [data-aid]> 
    <span.verse-number.verse> 
    {text} 
    {text} 
    <a.footnote.study-note-ref [href, rel]> 
    <sup.studyNoteMarker.dontHighlight> 
     {text} 
    {text} 
    {text} 
    <a.footnote.study-note-ref [href, rel]> 
    <sup.studyNoteMarker.dontHighlight> 
     {text} 
    {text} 
    {text} 

だから、一つの解決策は、(それが厄介なハックのビットです)ノードセットからそれらを削除し、その後、supノードを抽出することです。

footnotes <- page %>% 
    html_nodes(xpath = "//p[@id = 'p3']//sup") 

xml_remove(footnotes) 
page %>% 
    html_nodes(xpath = "//p[@id = 'p3']") %>% 
    html_text() 

[1] "3 And God said, Let there be light: and there was light." 
関連する問題