【问题标题】:R webscraping - links and urlsR webscraping - 链接和网址
【发布时间】:2016-12-07 11:31:40
【问题描述】:

我有一个类似于 Scraping a web page, links on a page, and forming a table with R 的问题。我会将此作为对该主题的评论发布,但我的得分还不够。

我有以下代码:

## 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 'slectorgadget'
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)

此时,我想从我获得的 url 中提取文本并添加为右侧的一列,然后为我需要的其他内容执行此操作。然而,当我编译时

FAO_Countries_data_text <- FAO_Countries_data$FAO_Countries_urls %>%
html_nodes("#foodSecurity-1") %>%
html_text()

我收到以下错误消息:

Error in UseMethod("xml_find_all") : 
no applicable method for 'xml_find_all' applied to an object of class "character"

换句话说,我无法从新建的数据框中获取链接。

现在,我有一个如下所示的数据框:

> head(FAO_Countries_data, n=3)
  FAO_Countries_links                  FAO_Countries_urls
  1         Afghanistan /countryprofiles/index/en/?iso3=AFG
  2             Albania /countryprofiles/index/en/?iso3=ALB
  3             Algeria /countryprofiles/index/en/?iso3=DZA

我将通过添加包含各种 url 中存在的信息的列来扩展此数据框。例如:

FAO_Countries_links                  FAO_Countries_urls      Food_security
  1         Afghanistan /countryprofiles/index/en/?iso3=AFG Family farming

【问题讨论】:

  • 您为html_nodes 提供了一个字符向量,但它需要一个文档、一个节点集或单个节点。不清楚您所说的 “来自网址的文本” 是什么意思,因为您已经在 FAO_Countries_data$FAO_Countries_links (?) 中获得了锚文本。
  • 感谢您的回复。不过,我不明白为什么要为 html_nodes() 提供字符向量。
  • FAO_Countries_data$FAO_Countries_urls 是一个字符向量(一堆字符串),而不是一个节点集(一个特殊的 xml 对象)。没什么好说的。那么您介意说出“来自网址的文本”是什么意思吗?或者,换句话说,你希望你的最终结果是什么样的?
  • 好的,现在我的数据框中有两列。一个用于链接,另一个用于网址。在任何 urls 页面中,都有一些部分的文本我想推断并放入靠近我已有的列的列中。例如,在任何国家/地区都有一个致力于食品安全的部门,我想将其文本添加到我的数据集中。我希望我说清楚了。非常感谢
  • 好的,所以你想read_html你的每个链接并从那里提取更多信息。例如,文本是什么? “fao.org/countryprofiles/index/en/?iso3=ECU”中的“#foodSecurity-1”?

标签: r url web-scraping


【解决方案1】:

使用下面的代码,我可以提取 5 个国家的“新闻项目”、“gsa-publication”和“projectsCountry”的文本:

library(stringr)
library(rvest)
library(RDCOMClient)

## Import web page
FAO_Countries <- read_html("http://www.fao.org/countryprofiles/en/")
FAO_Countries_urls <- FAO_Countries %>% html_nodes(".linkcountry") %>% html_attr("href")
FAO_Countries_links <- FAO_Countries %>% html_nodes(".linkcountry") %>% html_text()
FAO_Countries_data <- data.frame(FAO_Countries_links = FAO_Countries_links, 
                                 FAO_Countries_urls = FAO_Countries_urls, stringsAsFactors = FALSE)

url <- paste0("http://www.fao.org", FAO_Countries_data$FAO_Countries_urls) 

IEApp <- COMCreate("InternetExplorer.Application")
IEApp[['Visible']] <- TRUE
list_News_Text <- list()
list_GSA_Publication <- list()
list_ProjectsCountry <- list()

for(i in 1 : 5)
{
  print(i)
  IEApp$Navigate(url[i])
  
  Sys.sleep(10)
  
  doc <- IEApp$Document()
  html_Content <- doc$documentElement()$innerText()
  web_Obj <- doc$getElementByID("newsItems")
  list_News_Text[[i]] <- web_Obj$innerText()
  web_Obj <- doc$getElementByID("gsa-publications")
  list_GSA_Publication[[i]] <- web_Obj$innerText()
  web_Obj <- doc$getElementByID("projectsCountry")
  list_ProjectsCountry[[i]] <- web_Obj$innerText()
}

print(list_News_Text)

您可以使用类似的方法来提取不同网页的其他项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多