中的R - 与rvest爬行 - 用失败HTML_TEXT使用rvest功能
问题描述:
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”(我不知道它是什么)。
因此,如果有人告诉我如何解决它或让我知道我可以在谷歌上找到的搜索关键字来解决这个问题。
答
它动态地构建了相当多的页面。这应该有所帮助。
文章内容位于XML文件中。该URL可以从contid
参数构建。无论是传递一个完整的文章HTML URL(像在您的示例)或只有contid
价值这一点,它会返回一个xml2
xml_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>
## ...
注:我戳周围的网站看到这违反了他们的服务条款,它似乎并没有,但我也依靠谷歌翻译,它可能已经很难找到。确保您可以合法地(并且道德上讲,如果您关心道德操守)根据您的意图刮掉此内容,这一点非常重要。
感谢您对您的技术帮助和谨慎的建议。两者都非常有帮助。我会特别注意你的预防措施。再次感谢你。 –