2017-09-17 46 views
0
url <-"http://news.chosun.com/svc/content_view/content_view.html?contid=1999080570392" 

hh = read_html(GET(url),encoding = "EUC-KR") 

#guess_encoding(hh) 

html_text(html_node(hh, 'div.par')) 
#html_text(html_nodes(hh ,xpath='//*[@id="news_body_id"]/div[2]/div[3]')) 

我試圖抓取新聞數據(只是爲了練習)在河中的R - 與rvest爬行 - 用失敗HTML_TEXT使用rvest功能

當我試圖讓在HTML標籤的文本它在上面的主頁上,我沒有從網頁上獲取文本。 (Xpath也不工作)

我不認爲我沒有找到包含我想要在頁面上獲得的文本的鏈接。但是,當我嘗試使用html_text函數從該鏈接中提取文本時,它將被提取爲「」或空格。

我找不到原因..我沒有任何HTML和爬行經驗。

我猜的是包含新聞正文上下文的HTML標籤,有「class」和「data-dzo」(我不知道它是什麼)。

因此,如果有人告訴我如何解決它或讓我知道我可以在谷歌上找到的搜索關鍵字來解決這個問題。

回答

2

它動態地構建了相當多的頁面。這應該有所幫助。

文章內容位於XML文件中。該URL可以從contid參數構建。無論是傳遞一個完整的文章HTML URL(像在您的示例)或只有contid價值這一點,它會返回一個xml2xml_document與解析的XML結果:

#' Retrieve article XML from chosun.com 
#' 
#' @param full_url_or_article_id either a full URL like 
#'  `http://news.chosun.com/svc/content_view/content_view.html?contid=1999080570392` 
#'  or just the id (e.g. `1999080570392`) 
#' @return xml_document 
read_chosun_article <- function(full_url_or_article_id) { 

    require(rvest) 
    require(httr) 

    full_url_or_article_id <- full_url_or_article_id[1] 

    if (grepl("^http", full_url_or_article_id)) { 
    contid <- httr::parse_url(full_url_or_article_id) 
    contid <- contid$query$contid 
    } else { 
    contid <- full_url_or_article_id 
    } 

    # The target article XML URLs are in the following format: 
    # 
    # http://news.chosun.com/priv/data/www/news/1999/08/05/1999080570392.xml 
    # 
    # so we need to construct it from substrings in the 'contid' 

    sprintf(
    "http://news.chosun.com/priv/data/www/news/%s/%s/%s/%s.xml", 
    substr(contid, 1, 4), # year 
    substr(contid, 5, 6), # month 
    substr(contid, 7, 8), # day 
    contid 
) -> contid_xml_url 

    res <- httr::GET(contid_xml_url) 

    httr::content(res) 

} 

read_chosun_article("http://news.chosun.com/svc/content_view/content_view.html?contid=1999080570392") 
## {xml_document} 
## <content> 
## [1] <id>1999080570392</id> 
## [2] <site>\n <id>1</id>\n <name><![CDATA[www]]></name>\n</site> 
## [3] <category>\n <id>3N1</id>\n <name><![CDATA[사람들]]></name>\n <path ... 
## [4] <type>0</type> 
## [5] <template>\n <id>2006120400003</id>\n <fileName>3N.tpl</fileName> ... 
## [6] <date>\n <created>19990805192041</created>\n <createdFormated>199 ... 
## [7] <editor>\n <id>chosun</id>\n <email><![CDATA[[email protected] ... 
## [8] <source><![CDATA[0]]></source> 
## [9] <title><![CDATA[[동정] 이철승, 순국학생 위령제 지내 등]]></title> 
## [10] <subTitle/> 
## [11] <indexTitleList/> 
## [12] <authorList/> 
## [13] <masterId>1999080570392</masterId> 
## [14] <keyContentId>1999080570392</keyContentId> 
## [15] <imageList count="0"/> 
## [16] <mediaList count="0"/> 
## [17] <body count="1">\n <page no="0">\n <paragraph no="0">\n  <t ... 
## [18] <copyright/> 
## [19] <status><![CDATA[RL]]></status> 
## [20] <commentBbs>N</commentBbs> 
## ... 

read_chosun_article("1999080570392") 
## {xml_document} 
## <content> 
## [1] <id>1999080570392</id> 
## [2] <site>\n <id>1</id>\n <name><![CDATA[www]]></name>\n</site> 
## [3] <category>\n <id>3N1</id>\n <name><![CDATA[사람들]]></name>\n <path ... 
## [4] <type>0</type> 
## [5] <template>\n <id>2006120400003</id>\n <fileName>3N.tpl</fileName> ... 
## [6] <date>\n <created>19990805192041</created>\n <createdFormated>199 ... 
## [7] <editor>\n <id>chosun</id>\n <email><![CDATA[[email protected] ... 
## [8] <source><![CDATA[0]]></source> 
## [9] <title><![CDATA[[동정] 이철승, 순국학생 위령제 지내 등]]></title> 
## [10] <subTitle/> 
## [11] <indexTitleList/> 
## [12] <authorList/> 
## [13] <masterId>1999080570392</masterId> 
## [14] <keyContentId>1999080570392</keyContentId> 
## [15] <imageList count="0"/> 
## [16] <mediaList count="0"/> 
## [17] <body count="1">\n <page no="0">\n <paragraph no="0">\n  <t ... 
## [18] <copyright/> 
## [19] <status><![CDATA[RL]]></status> 
## [20] <commentBbs>N</commentBbs> 
## ... 

注:我戳周圍的網站看到這違反了他們的服務條款,它似乎並沒有,但我也依靠谷歌翻譯,它可能已經很難找到。確保您可以合法地(並且道德上講,如果您關心道德操守)根據您的意圖刮掉此內容,這一點非常重要。

+0

感謝您對您的技術幫助和謹慎的建議。兩者都非常有幫助。我會特別注意你的預防措施。再次感謝你。 –