2017-11-25 208 views
0

我想从联合国粮农组织网站(http://www.fao.org/countryprofiles/en/)建立一个数据集。 在此页面中包含一组指向国家的链接。 点击此链接的任何一个链接将导致包含国家/地区新闻的特定国家/地区的页面。 这个想法是在数据集包括:R网络扫描数据集

Country name 
Country url (e.g. <http://www.fao.org/countryprofiles/index/en/?iso3=AFG>) 
News url (e.g. <http://www.fao.org/afghanistan/news/detail-events/en/c/1045264/>) 
News title (e.g. World Food Day 2017 Celebrations in Afghanistan) 
News date (e.g. 17/11/2017) 

然而,我也做了以下内容:

## Import web page 
FAO_Countries <- read_html("http://www.fao.org/countryprofiles/en/") 

## Import the urls I am interested in with 'selectorgadget' 
FAO_Countries_urls <- FAO_Countries %>% 
html_nodes(".linkcountry") %>% 
html_attr("href") 

## Import the links I am interested in with 'selectorgadget' 
FAO_Countries_links <- FAO_Countries %>% 
html_nodes(".linkcountry") %>% 
html_text() 

## I create a dataframe with two previous objects 
FAO_Countries_data <- data.frame(FAO_Countries_links=FAO_Countries_links, 
FAO_Countries_urls = FAO_Countries_urls, stringsAsFactors = FALSE) 

我应该如何进行?

+1

你应该:a)列出你已经装包,和b)表明你有什么困难。 –

+0

1.包: rvest, stringr, tidyr, data.table, plyr, XML2。 2.我无法获取新闻和新闻日期 – Ileeo

回答

0

下面是解...

,如果你想获得整个消息,因为所有国家都应该动态地更改短名称(即美国,瑞典等)。 AFG的一个例子如下所述。

library(jsonlite) 

    shortname <-"AFG" 
    news_url <- paste0("http://www.fao.org/countryprofiles/common/allnews/en/?iso3=",shortname,"&allnews=no&limit=2") 
    news<- fromJSON(news_url) 

    title <- news[3] 

    date <- news[6] 

    cbind(date,title) 

     date_format                          title 
    1 17/10/2017               World Food Day 2017 Celebrations in Afghanistan 
    2 16/10/2017 On World Food Day, the future of migration and rural development is highlighted in the Asia-Pacific region 

如果你想要得到这个消息的内容,你可以添加以下代码:

content <- news[5]