簡體   English   中英

在R中-使用rvest進行爬網-無法使用html_text函數獲取HTML標記中的文本

[英]in R - crawling with rvest - fail to get the texts in HTML tag using html_text function

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抓取新聞數據(僅用於練習)。

在上面的首頁上嘗試時,我無法從頁面中獲取文本。 (Xpath也不起作用。)

我不認為我找不到包含要在頁面上獲取的文本的鏈接。 但是,當我嘗試使用html_text函數從該鏈接中提取文本時,它會提取為“”或空白。

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

我猜的是包含新聞正文上下文的HTML標記,具有“類”和“ data-dzo”(我不知道它是什么)。

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

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

文章內容在XML文件中。 可以從contid參數構造URL。 要么傳遞完整的文章HTML URL(例如您的示例中的URL),要么僅contid值,它將返回帶有已解析的XML結果的xml2 xml_document

#' 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[webmaster@chosun.com ...
##  [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[webmaster@chosun.com ...
##  [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>
## ...

注意:我在該網站上閑逛,發現這違反了他們的服務條款,但似乎沒有,但我也依靠google翻譯,這可能使查找變得更加困難。 重要的是要確保您可以合法地(並且從道德上講,如果您在乎道德)刮擦此內容以用於您打算的任何用途。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM