【问题标题】:Multiple web table mining with R, RCurl使用 R、RCurl 进行多 Web 表挖掘
【发布时间】:2015-01-10 23:43:01
【问题描述】:

首先,提前感谢您的任何回复。

我需要通过在各自的网页中加入一些较小的表格来获取表格。迄今为止,我已经能够提取信息,但未能使用循环自动完成。 迄今为止,我的命令是:

library(RCurl)
library(XML)
# index <- toupper(letters)
# EDIT:
index <- LETTERS

index[1] <- "0-A"
url <- paste("www.citefactor.org/journal-impact-factor-list-2014_", index, ".html", sep="", collapse=";")
urls <- strsplit(url, ";") [[1]]

这是我的循环尝试:

read.html.tab <- function(url){
 require(RCurl)
 require(XML)
 uri <- url
 tabs <- NULL
 for (i in uri){
  tabs <- getURL(uri)
  tabs <- readHTMLTable(tabs, stringsAsFactors = F)
  tab1 <- as.data.frame(tabs)
  }
 tab1
 }

如果我尝试使用read.html.tab 函数:

tab0 <- read.html.tab(urls)

我收到以下错误: Error in data.frame(`Search Journal Impact Factor List 2014` = list(`0-A` = "N", : arguments imply differing number of rows: 1, 1100, 447, 874, 169, 486, 201, 189, 172, 837....

但是,如果urls 只有一个元素,则该函数有效:

tabA <- read.html.tab(urls[1])
tabB <- read.html.tab(urls[2]) 
tab.if <- rbind(tabA,tabB)

ifacs <- tab.if[,27:ncol(tab.if)]
View(ifacs)

看来我不明白循环是如何工作的......

【问题讨论】:

    标签: r for-loop web-scraping rcurl


    【解决方案1】:

    Hadleyverse 的强制性答案:

    library(rvest)
    library(dplyr)
    library(magrittr)
    library(pbapply)
    
    urls <- sprintf("http://www.citefactor.org/journal-impact-factor-list-2014_%s.html", 
                    c("0-A", LETTERS[-1]))
    
    dat <- urls %>%
      pblapply(function(url) 
        html(url) %>% html_table(header=TRUE) %>% extract2(2)) %>%
      bind_rows()
    
    glimpse(dat)
    
    ## Observations: 1547
    ## Variables:
    ## $ INDEX     (int) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,...
    ## $ JOURNAL   (chr) "4OR-A Quarterly Journal of Operations Researc...
    ## $ ISSN      (chr) "1619-4500", "0891-0162", "0149-1423", "1550-7...
    ## $ 2013/2014 (chr) "0.918", "0.608", "1.832", "3.905", "1.776", "...
    ## $ 2012      (chr) "0.73", "0.856", "1.768", "4.386", "1.584", "0...
    ## $ 2011      (chr) "0.323", "0.509", "1.831", "5.086", "1.432", "...
    ## $ 2010      (chr) "0.69", "0.56", "1.964", "3.942", "1.211", "0....
    ## $ 2009      (chr) "0.75", "-", "1.448", "3.54", "1.19", "0.293",...
    ## $ 2008      (chr) "-", "-", "1.364", "-", "1.445", "0.352", "1.4...
    

    rvest 给我们htmlhtml_table

    我将magrittr 仅用于extract2,它只是包装[[ 并且读起来更好(IMO)。

    pbapply 包包装了*apply 函数并为您提供免费的进度条。

    注意:bind_rows 是最新的dplyr,因此请在使用前抓住它。

    【讨论】:

      【解决方案2】:

      您可能完全放弃 for 循环并使用以下内容:

      Data <- lapply(urls, function(x){
        readHTMLTable(
          getURL(x),
          stringsAsFactors=F)[[2]]
      })
      

      这将为您提供data.frames 的列表 -

      R> class(Data)
      [1] "list"
      R> length(Data)
      [1] 26
      R> head(Data[[1]])
        INDEX                                        JOURNAL      ISSN 2013/2014  2012  2011  2010  2009  2008
      1     1 4OR-A Quarterly Journal of Operations Research 1619-4500     0.918  0.73 0.323  0.69  0.75     -
      2     2                                  Aaohn Journal 0891-0162     0.608 0.856 0.509  0.56     -     -
      3     3                                  Aapg Bulletin 0149-1423     1.832 1.768 1.831 1.964 1.448 1.364
      4     4                                   AAPS Journal 1550-7416     3.905 4.386 5.086 3.942  3.54     -
      5     5                              Aaps Pharmscitech 1530-9932     1.776 1.584 1.432 1.211  1.19 1.445
      6     6                                   Aatcc Review 1532-8813     0.254 0.354 0.139 0.315 0.293 0.352
      

      我不确定您是否想将它们全部合并到一个对象中,但如果是的话,您可以使用do.call(rbind,Data)。另外,我认为这些 url 中的每一个都返回了两个表,第一个在页面顶部开始搜索目录,这就是我使用

      的原因
      readHTMLTable(
          getURL(x),
          stringsAsFactors=F)[[2]]
      

      lapply 内,而不是

      readHTMLTable(
              getURL(x),
              stringsAsFactors=F)
      

      后者会为每个 url 返回一个包含两个表的列表 -

      R> head(url1[[1]])
        0-A &nbsp| B &nbsp| C &nbsp| D &nbsp| E &nbsp| F &nbsp| G &nbsp| H &nbsp| I &nbsp| J &nbsp| K &nbsp| L &nbsp| M &nbsp|
      1   N &nbsp| O &nbsp| P &nbsp| Q &nbsp| R &nbsp| S &nbsp| T &nbsp| U &nbsp| V &nbsp| W &nbsp| X &nbsp| Y &nbsp| Z &nbsp|
      ##
      R> head(url1[[2]])
        INDEX                                        JOURNAL      ISSN 2013/2014  2012  2011  2010  2009  2008
      1     1 4OR-A Quarterly Journal of Operations Research 1619-4500     0.918  0.73 0.323  0.69  0.75     -
      2     2                                  Aaohn Journal 0891-0162     0.608 0.856 0.509  0.56     -     -
      3     3                                  Aapg Bulletin 0149-1423     1.832 1.768 1.831 1.964 1.448 1.364
      4     4                                   AAPS Journal 1550-7416     3.905 4.386 5.086 3.942  3.54     -
      5     5                              Aaps Pharmscitech 1530-9932     1.776 1.584 1.432 1.211  1.19 1.445
      6     6                                   Aatcc Review 1532-8813     0.254 0.354 0.139 0.315 0.293 0.352
      

      【讨论】:

      • 非常感谢!,它的工作原理!我一直在查看您对lapply 的使用,它属于我有时会遇到困难的一类功能。唯一的问题:如何获得url1
      • 不客气; url1 只是我制作的一个临时对象,用于显示没有 [[2]]readHTMLTable(getURL(x)) 会返回什么。
      猜你喜欢
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2015-05-15
      • 2013-11-17
      • 1970-01-01
      相关资源
      最近更新 更多