2017-11-25 209 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]