【发布时间】:2019-10-22 18:35:36
【问题描述】:
我正在尝试从 RSS 提要获取 WSJ 文章的链接。
Feed 如下所示:
<rss xmlns:wsj="http://dowjones.net/rss/" xmlns:dj="http://dowjones.net/rss/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>WSJ.com: World News</title>
<link>http://online.wsj.com/page/2_0006.html</link>
<atom:link type="application/rss+xml" rel="self" href="http://online.wsj.com/page/2_0006.html"/>
<description>World News</description>
<language>en-us</language>
<pubDate>Mon, 09 Sep 2019 10:56:42 -0400</pubDate>
<lastBuildDate>Mon, 09 Sep 2019 10:56:42 -0400</lastBuildDate>
<copyright>Dow Jones & Company, Inc.</copyright>
<generator>http://online.wsj.com/page/2_0006.html</generator>
<docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
<image>
<title>WSJ.com: World News</title>
<link>http://online.wsj.com/page/2_0006.html</link>
<url>http://online.wsj.com/img/wsj_sm_logo.gif</url>
</image>
<item>
<title>
Boris Johnson Promises Oct. 31 Brexit as Law Passes to Rule Out No Deal
</title>
<link>
https://www.wsj.com/articles/boris-johnson-insists-he-wants-a-brexit-deal-despite-no-deal-planning-11568037248
</link>
<description>
<![CDATA[
British Prime Minister Boris Johnson stuck to his pledge that the U.K. would leave the European Union on Oct. 31—even as a bill aimed at preventing the country from leaving on that date without an agreement became law.
]]>
</description>
<content:encoded/>
<pubDate>Mon, 09 Sep 2019 10:46:00 -0400</pubDate>
<guid isPermaLink="false">SB10710731395272083797004585540162284821560</guid>
<category domain="AccessClassName">PAID</category>
<wsj:articletype>U.K. News</wsj:articletype>
</item>
<item>
<title>
Russian Opposition Puts Putin Under Pressure in Moscow Election
</title>
<link>
https://www.wsj.com/articles/russian-opposition-puts-putin-under-pressure-in-moscow-election-11568029495
</link>
<description>
<![CDATA[
Candidates backed by Russia’s opposition won nearly half the seats up for grabs in Moscow’s city elections Sunday, building on a wave of protests that exposed some of the frailties in President Putin’s closely controlled political machine, but failed to make significant inroads in local races elsewhere.
]]>
</description>
<content:encoded/>
<pubDate>Mon, 09 Sep 2019 07:44:00 -0400</pubDate>
<guid isPermaLink="false">SB10710731395272083797004585539862964447000</guid>
<category domain="AccessClassName">PAID</category>
<wsj:articletype>Russia News</wsj:articletype>
</item>
我一直在使用rvest 来获取每篇文章的标题,这很有效,但链接每次都返回空白。我用几种不同的方式尝试了代码,但这是最近的尝试:
rm(list=ls())
library(tidyverse)
library(rvest)
setwd("~/wsj/world_news")
wsj_1 <- "wsj-world_news-1568041806.39885.xml" # a file like the example one provided above
test <- wsj_1 %>% read_html() # reading in example file
items <- wsj_1 %>%
read_html() %>%
html_nodes('item') # parsing the xml to get each 'item' which is a separate article
title <- items %>%
html_nodes('title') %>%
html_text()
link <- items %>%
html_node('link') %>%
html_text()
知道为什么我无法显示链接吗?我得到<link> 而不是网址。
我也无法提取描述标签中的 CDATA 文本,但这不是我最关心的问题。如果我能得到链接就足够了。
【问题讨论】:
标签: r xml web-scraping rss rvest