【问题标题】:Is rvest the best tool to collect information from this table?rvest 是从该表中收集信息的最佳工具吗?
【发布时间】:2018-12-31 23:51:06
【问题描述】:

我使用 rvest 包提取了公司列表和每个公司中的 a.href 元素,我需要继续数据收集过程。这是网站的链接:http://www.bursamalaysia.com/market/listed-companies/list-of-companies/main-market

我已使用以下代码提取表格,但没有任何结果。我使用了其他方法,如“Scraping table of NBA stats with rvest”和类似链接中发布的方法,但我无法获得我想要的。任何帮助将不胜感激。

我的代码:

link.main <- 
 "http://www.bursamalaysia.com/market/listed-companies/list-of-companies/main-market/"

web <- read_html(link.main) %>% 
html_nodes("table#bm_equities_prices_table") 
    # it does not work even when I write html_nodes("table") 
    or ".table" or #bm_equities_prices_table

web <- read_html(link.main) 
        %>% html_nodes(".bm_center.bm_dataTable") 
  # no working

web <- link.main %>% read_html() %>% html_table() 
  # to inspect the position of table in this website 

【问题讨论】:

    标签: css r dynamic web-scraping rvest


    【解决方案1】:

    页面使用 JavaScript 生成表格,因此您需要使用 RSelenium 或 Python 的 Beautiful Soup 来模拟浏览器会话并允许 JavaScript 运行。

    另一种选择是使用 @hrbrmstr 的名为 decapitated 的超赞包,它基本上在后台运行无头 Chrome 浏览器会话。

    #devtools::install_github("hrbrmstr/decapitated")
    
    library(decapitated)
    library(rvest)
    
    res <- chrome_read_html(link.main)
    
    main_df <- res %>% 
      rvest::html_table() %>%
      .[[1]] %>%
      as_tibble()
    

    这样就可以输出表格的内容了。如果您想访问表格下面的元素(表格文本后面的href 属性),您需要做更多的列表体操。表格中的某些元素实际上是缺少链接的,事实证明用 css 提取很困难。

    library(dplyr)
    library(purrr)
    
    href_lst <- res %>% 
      html_nodes("table td") %>% 
      as_list() %>% 
      map("a") %>% 
      map(~attr(.x, "href"))
    
    # we need every third element starting from second element
    idx <- seq.int(from=2, by=3, length.out = nrow(main_df))
    
    href_df <- tibble(
      market_href=as.character(href_lst[idx]),
      company_href=as.character(href_lst[idx+1])
    )
    
    bind_cols(main_df, href_df)
    
    #> # A tibble: 800 x 5
    #>       No `Company Name`   `Company Website` market_href   company_href
    #>    <int> <chr>            <chr>             <chr>         <chr>       
    #>  1     1 7-ELEVEN MALAYS~ http://www.7elev~ /market/list~ http://www.~
    #>  2     2 A-RANK BERHAD [~ http://www.arank~ /market/list~ http://www.~
    #>  3     3 ABLEGROUP BERHA~ http://www.gefun~ /market/list~ http://www.~
    #>  4     4 ABM FUJIYA BERH~ http://www.abmfu~ /market/list~ http://www.~
    #>  5     5 ACME HOLDINGS B~ http://www.suppo~ /market/list~ http://www.~
    #>  6     6 ACOUSTECH BERHA~ http://www.acous~ /market/list~ http://www.~
    #>  7     7 ADVANCE SYNERGY~ http://www.asb.c~ /market/list~ http://www.~
    #>  8     8 ADVANCECON HOLD~ http://www.advan~ /market/list~ http://www.~
    #>  9     9 ADVANCED PACKAG~ http://www.advan~ /market/list~ http://www.~
    #> 10    10 ADVENTA BERHAD ~ http://www.adven~ /market/list~ http://www.~
    #> # ... with 790 more rows
    

    【讨论】:

    【解决方案2】:

    不使用浏览器的另一种选择:

    library(httr)
    library(jsonlite)
    library(XML)
    r <- httr::GET(paste0(
        "http://ws.bursamalaysia.com/market/listed-companies/list-of-companies/list_of_companies_f.html",
        "?_=1532479072277",
        "&callback=jQuery16206432131784246533_1532479071878",
        "&alphabet=",
        "&market=main_market",
        "&_=1532479072277"))
    l <- rawToChar(r$content)
    m <- gsub("jQuery16206432131784246533_1532479071878(", "", substring(l, 1, nchar(l)-1), fixed=TRUE)
    tbl <- XML::readHTMLTable(jsonlite::fromJSON(m)$html)$bm_equities_prices_table
    

    输出:

    > head(tbl)
    #  No                      Company Name                Company Website
    #1  1 7-ELEVEN MALAYSIA HOLDINGS BERHAD      http://www.7eleven.com.my
    #2  2                 A-RANK BERHAD [S]        http://www.arank.com.my
    #3  3              ABLEGROUP BERHAD [S]       http://www.gefung.com.my
    #4  4             ABM FUJIYA BERHAD [S]    http://www.abmfujiya.com.my
    #5  5          ACME HOLDINGS BERHAD [S] http://www.supportivetech.com/
    #6  6              ACOUSTECH BERHAD [S]   http://www.acoustech.com.my/
    

    【讨论】:

    • 您需要提及从何处获取您在此处粘贴的链接。按 Ctrl+Shift+I(Win 上的 F12)在 Chrome 中打开开发人员工具。转到网络选项卡并检查运行时间最长的查询。获取请求 URL。您应该能够立即在 R 中使用它(尽管使用适当的安全令牌应该很快过期)i.stack.imgur.com/ZXHYK.png
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2011-11-24
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多